Phase: 5. OOP | Estimated time: 1.5 hours | Milestone Project: No
- Module 048 (Dunder Methods)
By the end of this module, you will be able to:
- Use the
abcmodule to create abstract base classes - Define abstract methods with
@abstractmethod - Explain why ABCs exist (enforcing interface contracts)
- Create concrete subclasses that implement abstract methods
- Define abstract properties
- Register virtual subclasses with
.register()
Abstract Base Classes let you define interfaces — contracts that subclasses must fulfill. This ensures consistency across implementations and catches missing methods early.
abc stands for Abstract Base Classes. Use ABC as a parent and @abstractmethod to mark required methods:
from abc import ABC, abstractmethod
class Shape(ABC):
"""Abstract base class for shapes."""
@abstractmethod
def area(self):
"""Calculate area. Must be implemented by subclasses."""
...
@abstractmethod
def perimeter(self):
"""Calculate perimeter. Must be implemented."""
...Any class that inherits from an ABC must implement all abstract methods, or it cannot be instantiated:
class Circle(Shape):
"""Concrete circle class."""
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius ** 2
def perimeter(self):
return 2 * 3.14159 * self.radius
circle = Circle(5)
print(circle.area()) # 78.53975
print(circle.perimeter()) # 31.4159# This would raise TypeError:
# class Incomplete(Shape):
# pass
# obj = Incomplete() # Can't instantiate abstract class- Enforce contracts: Subclasses must implement specified methods.
- Documentation: Clearly states what methods are expected.
- Early error detection: Fails at instantiation time, not at method call time.
- Polymorphism: You can write functions that accept any
Shape.
def print_shape_info(shape: Shape):
"""Print area and perimeter of any Shape."""
if not isinstance(shape, Shape):
raise TypeError("Must be a Shape")
print(f"Area: {shape.area()}, Perimeter: {shape.perimeter()}")You can also require properties in subclasses:
from abc import ABC, abstractmethod
class Employee(ABC):
"""Abstract employee class."""
@property
@abstractmethod
def role(self):
"""Role must be defined as a property."""
...
@abstractmethod
def calculate_pay(self):
"""Calculate employee pay."""
...
class Manager(Employee):
"""Concrete manager class."""
@property
def role(self):
return "Manager"
def calculate_pay(self):
return 80000
m = Manager()
print(m.role) # Managerregister() lets you mark a class as a subclass without inheritance. This is useful for integration with third-party code.
from abc import ABC
class Iterable(ABC):
"""Abstract iterable."""
@abstractmethod
def __iter__(self):
...
# Register an existing class
Iterable.register(list)
Iterable.register(tuple)
print(isinstance([1, 2, 3], Iterable)) # TruePython uses ABCs extensively in collections.abc:
| ABC | Requires | Used by |
|---|---|---|
Iterable |
__iter__ |
for loops |
Sequence |
__getitem__, __len__ |
Indexable collections |
Mapping |
__getitem__, __len__, __iter__ |
Dict-like objects |
Set |
__contains__, __iter__, __len__ |
Set-like objects |
Callable |
__call__ |
Callable objects |
- Forgetting
@abstractmethod: Without it, the method is just a regular method that can be left unimplemented. - Instantiating an ABC: Direct instantiation raises
TypeError. - Not implementing all abstract methods: The class becomes abstract too and cannot be instantiated.
- Overusing ABCs: For simple interfaces, duck typing may be sufficient.
- Create an ABC
Mediawith abstract methodsplay()andstop(). - Implement
Audio(Media)andVideo(Media)with concrete versions. - Write a function that accepts any
Mediaand callsplay(). - Register a third-party class as a virtual subclass.
- Use
collections.abc.Sequenceto check if a custom class behaves like a sequence.
- ABCs define interfaces that subclasses must implement.
@abstractmethodmarks methods that must be overridden.- Concrete subclasses must implement all abstract methods to be instantiable.
- Abstract properties enforce property contracts.
.register()marks virtual subclasses without inheritance.collections.abcprovides ready-made ABCs for common protocols.
Continue to Module 050: Milestone Project: Library/Inventory Management System.