A real class and a test fake, one Protocol
Neither storage class inherits Storage. The ABC at the end shows the other style: inheritance required, error at creation time.
A Protocol describes the methods and attributes an object must have, and anything that has them qualifies, with no inheritance. It is duck typing that a checker can verify: "if it quacks like this, it fits".
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a Protocol Greeter with greet(self, name: str) -> str and a class that satisfies it. Write welcome(g: Greeter) that prints g.greet("Asha").
Write a Protocol HasLength with __len__, and a function that returns "empty" or "not empty" for any object with a length. Call it with a list and a string.
Write a Protocol Clock with now(self) -> str and a FixedClock for tests that always returns "12:00".
Explain it without notes
What is structural typing, and how does a Protocol give it to Python?
How does a Protocol differ from an abstract base class? When do you pick each?
What does @runtime_checkable let you do, and what is the catch?
Why do Protocols make code easier to test?
A function accepts a Handler Protocol with __call__. When would you choose that over Callable[[str], bool]?
Expected output
Asha
fake ['save name', 'load name']
ABC: Can't instantiate abstract class Impl without an implementation for abstract method 'save'