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.
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.
If a callable feels like a function, we often call it a function... even when it's not!
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.
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.
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.
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.
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.
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.
While it is possible to make singleton objects in Python, the classic singleton design pattern doesn't always make a lot of sense.
Most Python objects store their attributes in a __dict__ dictionary. Modules and classes always use __dict__, but not everything does.
Most Python objects store their attributes in a __dict__ dictionary. Modules and classes always use __dict__, but not everything does.
While you don't often need to make your own classes in Python, they can sometimes make your code reusable and easier to read.
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.
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).
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?
Dynamically accessing attributes with the built-in getattr function in Python.
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.
Class methods are most often used for making alternate class constructors.
Static methods are functions that live on a class but don't know anything about the class.
Python's classes can have attributes, just as class instances can have attributes. What are class attributes usually used for?
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!