You’ll be able to
- Define classes with
classand initialize with__init__ - Create instances and access instance attributes
- Write instance methods that operate on
self - Understand the difference between class and instance state
Loading lesson…
Why this matters
Every serious framework — Django models, SQLAlchemy declarative base, Pydantic BaseModel, PyTorch nn.Module — is class-based. Understanding self, __init__, and method resolution order is what separates script writers from library authors.
Common pitfalls
- Mutable default arguments like
def __init__(self, items=[])— shared across instances; useNonesentinel instead. - Forgetting
selfon method calls or attribute reads — Python won't warn; you'll getNameErrorat runtime. - Overriding
__eq__without__hash__— silently breakssetanddictmembership.