Protocols vs ABCs

P28.protocols-abcs.01 · Audience: guest, it-ml, language-pro · Prerequisites: Records: class vs dataclass vs namedtuple

Real LLM grading for this pageLLM grading (this page):

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.

Images, PDF or text. Kept on this device only.
Keeping your files on this device

Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.

Ctrl/Cmd + Enter to send
Rung 1 — structural typing with Protocol

Loading exercise…

Interview rung — Protocols vs ABCs (free-form)

Loading exercise…

My notes on this module

Loading your notes...

Where next?

Protocols vs ABCs — TransformerLab