Classes and Protocols
Building classes that feel like built-in types is the goal of this part. Python’s standard library is a long argument that good class design is about implementing the right protocols — not inheriting from the right base class.
23 A Pythonic Object is a single example carried through five sections: a
Vector2dclass that becomes printable, hashable, formattable, slottable, and private-aware. It’s the chapter where every special method finally meets a use case.24 Special Methods for Sequences extends that example to a multi-dimensional
Vector, implementing the full sequence protocol —__getitem__,__len__, slicing, formatting, hashing — so the object behaves liketuple.25 Interfaces, Protocols, and ABCs draws the line between Python’s two type systems. Nominal typing (you inherit from an
ABC) and structural typing (you implement the right methods) coexist. We’ll write aVowelclass twice, once each way, and see which is more Pythonic.26 Inheritance: For Better or for Worse is the cautionary chapter. The MRO, mixins, multiple inheritance, the diamond — all of it works, none of it is required. Most Python code reaches for composition first, and you should too.
27 More About Type Hints returns to type hints with the tools that matter at scale:
TypeVar,Generic,Protocol,overload,TypedDict. These are the hints that catch bugs you’d otherwise ship.