Skip to content

Lesson 4 of 9 · 21m

ABCs: Contracts With Teeth

Abstract base classes are for small contracts, shared implementation, and fast failure — not for recreating Java taxonomy diagrams in Python.

I once inherited a service where every repository inherited from six layers of abstract base classes. The inheritance tree was beautiful in the diagram and useless in production: unit tests instantiated the abstract parent, concrete classes overrode methods already overridden elsewhere, and nobody could tell which method actually ran. ABCs are useful when they encode a small contract with teeth, not when they reproduce a corporate org chart.

What an ABC buys you

from abc import ABC, abstractmethod

class Notifier(ABC):
    @abstractmethod
    def send(self, recipient: str, message: str) -> None:
        # Deliver message or raise a delivery-specific exception.

Instantiating a subclass that forgets send raises TypeError at construction time. That is the whole value: fail early, near the mistake, instead of at 2 a.m. when the alert pipeline silently drops messages. Documentation helps humans; abstract methods help the interpreter reject incomplete implementations.

ABCs can share implementation — carefully

class RetryNotifier(Notifier):
    attempts = 3

    def notify_with_retry(self, recipient: str, message: str) -> None:
        for attempt in range(1, self.attempts + 1):
            try:
                return self.send(recipient, message)
            except TransientDeliveryError:
                if attempt == self.attempts:
                    raise

Shared implementation is legitimate when it is a template built on abstract primitives. It becomes toxic when the base class secretly depends on attributes defined only in some subclasses. If a base class requires self.batch_size but does not declare it, you have invented a hidden interface.

Registering virtual subclasses

ABCMeta.register lets you declare that a class satisfies a contract without inheriting. It is useful for integrating third-party types, but it is also a lie if the registered class does not truly implement the behavior. Structural checks do not happen automatically; registration is a claim. Prefer actual inheritance or protocols when you control the code.

Keep abstract surface area small

An interface with one or two abstract methods is easy to implement and easy to fake in tests. An interface with fifteen is not a contract; it is an implementation plan. Large ABCs encourage classes that implement half the methods by raising NotImplementedError, which moves failure from construction time into random runtime paths.

Field notes

  • ABCs are nominal: an object is a member because it inherits or is registered, not merely because it has the right methods.
  • Use ABCs when you need shared behavior plus enforcement; use protocols when you need only the shape.
  • Abstract properties exist, but overuse turns simple construction into ceremony.
  • Do not make every base class abstract 'for flexibility'; interfaces that no one implements are inventory, not design.

Was this lesson helpful?

Sign in to rate