Classes and Objects
- class and __init__. A class is a blueprint; __init__ runs on each new instance and sets its attributes through self.
- self. self is the instance itself, passed automatically as the first parameter of every method.
- Instance versus class attributes. Attributes set on self belong to one instance; attributes on the class are shared by all of them.
Inheritance, Composition and Protocols
- Inheritance. A subclass extends a parent, reusing behaviour and overriding what differs: super() reaches the parent’s version.
- Composition. Composition wires small objects together instead of subclassing: usually the more flexible design in Python.
- Duck typing. Python cares what an object can DO, not what it is: if it quacks, it passes: that is duck typing.
Dataclasses and Domain Models
- Dataclasses. The dataclass decorator writes __init__, __repr__ and __eq__ for you from the field declarations alone.
- Frozen dataclasses. A frozen dataclass is immutable: attempts to reassign a field raise, making value objects trustworthy.
- Modelling a domain. Good models name the business: an Order with items and a total beats three loose dicts passed in parallel.
Before and after this level
- Before: Level 5: Modules & Packages: Modules and Imports, Packages and Project Structure, Virtual Environments and Dependencies.
- Next: Level 7: Errors & Debugging: Exceptions, Debugging and Tracebacks, Designing Reliable Failure Handling.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 6: Object-Oriented Python cover?
Classes and Objects, Inheritance, Composition and Protocols, Dataclasses and Domain Models: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 5: Modules & Packages, which covers Modules and Imports, Packages and Project Structure, Virtual Environments and Dependencies.