Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In large-scale enterprise software architectures, enforcing interface contracts is essential. Developers need guarantees that components conform to expected behaviors.
Python provides two complementary architectural mechanisms for interface design:
abc.ABC): Nominal Subtyping (explicit inheritance where subclasses explicitly declare their parent).typing.Protocol - PEP 544): Structural Subtyping (static duck typing where an object matches if it possesses the required methods, regardless of its inheritance tree).abc.ABC & @abstractmethod)An Abstract Base Class cannot be instantiated directly. It forces all derived subclasses to implement designated abstract methods:
typing.Protocol)Python's philosophy has always celebrated duck typing: "If it walks like a duck and quacks like a duck, it's a duck."
With typing.Protocol (introduced in PEP 544), you get the compile-time type safety of an interface without requiring explicit class inheritance:
| Feature | Abstract Base Class (abc.ABC) | Protocol (typing.Protocol) |
|---|---|---|
| Typing Model | Nominal (explicit subclassing) | Structural (duck typing) |
| Code Sharing | Can provide concrete default implementations | Primarily declares interface signatures |
| Third-Party Types | Cannot retroactively make external classes inherit from your ABC | External classes naturally comply if signatures match |
| Primary Use Case | Framework class hierarchies (Django models, custom adapters) | Decoupled libraries, microservice contracts, DI |
@abstractmethod?Protocol to satisfy type checkers?isinstance(obj, MyProtocol) to work at runtime?In the next lesson, we will explore Metaclasses and Class Creation Hooks with __init_subclass__ (1: Advanced OOP Concepts).
👉 Continue to Next Lesson: Metaclasses and Class Creation Hooks with __init_subclass__ →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Multiple Inheritance | Metaclasses and Class Creation Hooks with __init_subclass__ |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.