Protocols vs ABCs
P28.protocols-abcs.01 · Audience: guest, it-ml, language-pro · Prerequisites: Records: class vs dataclass vs namedtuple
How do you say "any object that has these methods" in Python? Two answers:
structural typing with typing.Protocol — conformance by shape, no
inheritance — and nominal typing with abc.ABC — conformance by
subclassing. This track is about choosing between them, and it links forward to
P31 (Typing & Robust Code), where these tools meet mypy.
This first module makes duck typing explicit with a Protocol.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Rung 1 — structural typing with Protocol
Loading exercise…
Interview rung — Protocols vs ABCs (free-form)
Loading exercise…
The take-away: reach for a Protocol to type objects you don't own and to keep
implementations decoupled; reach for an ABC when you want shared code, a real
isinstance gate, or abstract methods enforced at instantiation.
Where next?
Later in Protocols & ABCs