27.7 Abstract Base Classes as a Contract
The Role of abc.ABC and @abstractmethod Abstract Base Classes (ABCs) in Python are not enforced by the language’s syntax at a fundamental level; rather, they are a design pattern implemented via the abc module. Their primary purpose is to define a formal contract, or interface, that derived classes must adhere to. The abc.ABC class serves as a convenient base class for creating ABCs. Using the @abstractmethod decorator on a method within an ABC declares that any concrete (i.e., non-abstract) subclass must provide an implementation for that method. This enforcement happens at the moment an instance of the subclass is created, not when the subclass itself is defined. This is a crucial distinction, as it allows for flexible class hierarchies where some abstract methods might be implemented by intermediate base classes.