Content · Programming · Python

Python Compare Protocols, ABCs, and Inheritance

Compare structural Protocols with inheritance and abstract base classes.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Compare structural Protocols with inheritance and abstract base classes.

What It Does and Why It Matters


A Protocol describes which operations an object must support; a static checker can accept an object that satisfies the interface without explicitly inheriting from the protocol. An ABC provides explicit nominal inheritance and can enforce abstract methods at instantiation time. Choose based on whether you need implementation reuse, runtime enforcement or flexible interoperability.

Runtime-checkable protocols have more limited checks than full static structural compatibility.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

from typing import Protocol
class Sender(Protocol):
    def send(self, text: str) -> None: ...
class ConsoleSender:
    def send(self, text: str) -> None:
        print(text)
def notify(sender: Sender) -> None:
    sender.send("ready")
notify(ConsoleSender())

How the Code Works


  • The imports make these standard-library tools available: typing.
  • The definition of Sender, ConsoleSender, notify creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

ready

For the demonstrated inputs, Python displays 'ready'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This helps maintain larger projects where functions and data shapes cross module boundaries. Check the code with a static analyzer, and still validate data received from files, users or remote APIs. Annotation constructs do not normally enforce types automatically at runtime.

Practice Exercise


Create a second sender that logs to a list and passes the same static type check.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Believing a Protocol always requires explicit subclassing or that a Protocol validates method signatures at runtime.
  • Overlooking the documented condition: a Protocol describes which operations an object must support; a static checker can accept an object that satisfies the interface without explicitly inheriting from the protocol.

Key Takeaways


  • Compare structural Protocols with inheritance and abstract base classes.
  • A Protocol describes which operations an object must support; a static checker can accept an object that satisfies the interface without explicitly inheriting from the protocol.
  • Create a second sender that logs to a list and passes the same static type check.
Share this resourceSend this page to someone who may find it useful.