2.8 Inheritance, composition and polymorphism
You can extend a library's class without breaking it.
Before:00. Orientation & SetupUnlocks:03. Data Handling & Analysis04. Classical AI — Agents, Search & Knowledge Representation
Inheritance and composition are the two ways to build on existing code, and both are needed to extend library classes — a PyTorch Dataset, an sklearn transformer — without breaking their contracts. Duck typing and Protocols are Python's looser take on interfaces. It follows the class basics because extension assumes them. The design lesson is composition over deep inheritance: hierarchies more than a couple of levels deep become archaeology, and most flexibility comes from having, not being.
Work through these
Single and multiple inheritance, MRO
Deriving one class from another, including from several at once, and the fixed order Python uses to resolve which method runs. That order is the part that surprises people.
super() and cooperative inheritance
The mechanism for calling a parent's implementation, which behaves differently from what most people expect when several parents are involved. Understanding it is what makes multiple inheritance survivable.
Composition over inheritance, in practice
Building objects out of other objects is usually more flexible than deriving from them, and this item makes the case with practical examples. It is the advice most experienced developers give.
Duck typing, ABCs and Protocols
Python cares what an object can do rather than what it inherits from, and there are two formal ways to state the expected behaviour. This is how libraries accept types they have never seen.
Sign in to keep your progress.
Free resources
We haven't checked most of these for screen reader use yet.
Links last checked 29 Aug 2026.
Stuck here?
Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.
Checking your session…
Topics shown in module order.