[Go to site: main page, start]

Classes

Classes are a way to bundle functionality and state together. The terms "type" and "class" are interchangeable: list, dict, tuple, int, str, set, and bool are all classes.

You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often.

21 articles · 1 hr 20 min read
18 screencasts · 1 hr 8 min watch
01
What is a class?
3 min read Screencast available

Classes are for coupling state (attributes) and functionality (methods). Calling a class returns an instance of that class. Class and "type" are synonyms in Python.

Read
Read
02
Classes are everywhere
3 min read Screencast available

Even if you never make your class, you will certainly use classes. A lot of the built-in functions in Python are classes too. Anything that has a type has a class.

Read
Read
03
Is it a class or a function?
4 min read Screencast available

If a callable feels like a function, we often call it a function... even when it's not!

Read
Read
04
Python's self
3 min read Screencast available

Python's self is really just a variable that points to the current instance of our class. Every method you define must accept self as its first argument.

Read
Read
05
What is __init__ in Python?
2 min read Screencast available

The __init__ method is used to initialize a class. The initializer method accepts self (the class instance) along with any arguments the class accepts and then performs initialization steps.

Read
Read
06
Docstrings
4 min read Screencast available

In Python we prefer docstrings to document our code rather than just comments. Docstrings must be the very first statement in their function, class, or module. Python's help function uses these.

Read
Read
07
Inheriting one class from another
4 min read Screencast available

To inherit your class from another class, put parentheses after the class name and list parent classes. We allow multiple inheritance in Python, but we usually prefer single class inheritance.

Read
Read
08
String Representations for Classes
6 min read

All Python objects have two different string representations, although we almost always use just one of them. You can customize both of these string representations on your Python objects.

Read
Read
09
dataclasses
2 min read

Python's dataclasses are a quick way to make a class with an initializer, a friendly string representation, and sensible equality checking. This class creation helper can also help make immutable classes, orderable classes, and more.

Read
Read
10
Singletons
5 min read Screencast available

While it is possible to make singleton objects in Python, the classic singleton design pattern doesn't always make a lot of sense.

Read
Read
11
__dict__: where Python stores attributes
6 min read Screencast available

Most Python objects store their attributes in a __dict__ dictionary. Modules and classes always use __dict__, but not everything does.

Read
Read
12
__slots__ for optimizing classes
5 min read Screencast available

Most Python objects store their attributes in a __dict__ dictionary. Modules and classes always use __dict__, but not everything does.

Read
Read
13
When are classes used in Python?
4 min read Screencast available

While you don't often need to make your own classes in Python, they can sometimes make your code reusable and easier to read.

Read
Read
14
Attributes are everywhere
2 min read Screencast available Premium

Attributes aren't just limited to classes. Class instances, classes, modules, and functions all have attributes! Anytime you see something.something_else, that's an attribute lookup.

Read
Read
15
Methods are just functions attached to classes
4 min read Screencast available Premium

Python's methods are just functions that happen to be attached to a class. When a function is attached to a class, the instance will automatically be passed in as the first argument (self).

Read
Read
16
How attribute lookups and assignments work
3 min read Screencast available Premium

Where does Python store attributes on class instances? And what happens when you assign an attribute on a class instance when that attribute already exists on its class?

Read
Read
17
Python's getattr function
4 min read Premium

Dynamically accessing attributes with the built-in getattr function in Python.

Read
Read
18
Where does Python look for methods?
4 min read Screencast available Premium

When we call a method on any object, Python will look at the type of that object and use its mro (method resolution order) to figure out which method to call. It checks the class first, then parents.

Read
Read
19
Class methods
4 min read Screencast available Premium

Class methods are most often used for making alternate class constructors.

Read
Read
20
Static methods
4 min read Screencast available Premium

Static methods are functions that live on a class but don't know anything about the class.

Read
Read
21
Using attributes on classes
3 min read Screencast available Premium

Python's classes can have attributes, just as class instances can have attributes. What are class attributes usually used for?

Read
Read
Continue exploring
Profile picture of Trey
Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨