Protocol — structural subtyping (duck typing with type safety).
1from typing import Protocol23class Drawable(Protocol):4 def draw(self) -> None: ...56class Circle:7 def draw(self) -> None:8 print("Drawing circle")910class Square:11 def draw(self) -> None:12 print("Drawing square")1314# Any class with draw() works — no inheritance needed15def render(shape: Drawable) -> None:16 shape.draw()1718render(Circle()) # OK19render(Square()) # OK20# render("text") # Type error: str has no draw()
vs ABC:
Use cases: