ABC and @abstractmethod block instantiation until every abstract method is implemented
abc.ABC is a class with ABCMeta as its metaclass. Methods decorated with @abstractmethod are recorded on the class, and ABCMeta refuses to instantiate any class that still has unimplemented abstract methods. That gives you a real interface contract: you get a TypeError at instantiation time, not a silent AttributeError later. ABCs also support virtual subclassing through register(), which lets a class be considered a subclass without inheritance. Note that abstractmethod does not prevent the method from having an implementation: subclasses can call super().method() to reuse shared behavior, and abstract properties and classmethods are also supported.
Use @abstractmethod for methods subclasses must implement; ABCMeta enforces instantiation rules.
Use @abstractproperty or property + abstractmethod for abstract attributes.
Use register() or subclasshook for virtual subclasses when you cannot modify the target class.
Trade-off: ABCs add runtime checks and a metaclass, which conflicts with other metaclasses. typing.Protocol offers structural typing as a lighter alternative.
Common mistake: expecting @abstractmethod to prevent calling the method. It does not; it only prevents instantiation of incomplete subclasses.
Common mistake: forgetting that an abstract class can still define concrete methods that subclasses inherit.
Version note: abc.ABC has been available since 3.4. typing.Protocol with @runtime_checkable has been available since 3.8 for structural checks.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience