How do abstract base classes and protocols enforce interfaces in Python?
· Category: Python Programming
Short answer
Abstract base classes (ABCs) from the abc module enforce that subclasses implement specific methods. typing.Protocol provides structural subtyping, allowing you to define interfaces without explicit inheritance.
Steps
- Inherit from
abc.ABCand decorate abstract methods with@abstractmethod. - Subclass the ABC and implement all abstract methods.
- Alternatively, define a
typing.Protocolclass with method stubs.
from abc import ABC, abstractmethod
class Drawable(ABC):
@abstractmethod
def draw(self):
pass
class Circle(Drawable):
def draw(self):
print("Drawing a circle")
# Protocol
from typing import Protocol
class Runnable(Protocol):
def run(self) -> None:
...
class Task:
def run(self):
print("Running task")
def execute(r: Runnable):
r.run()
execute(Task())
Tips
- ABCs prevent instantiation of incomplete classes, catching errors early.
Protocolis ideal for duck typing with static type checkers likemypy.collections.abcprovides many useful ABCs likeSequence,Mapping, andCallable.
Common issues
- Forgetting
@abstractmethodmeans the method is optional, defeating the purpose of the ABC. - ABCs add a small runtime overhead compared to plain classes.
Protocolchecks are enforced by static type checkers, not at runtime (unless explicitly checked withisinstance()and@runtime_checkable).