Skip to content

Lesson 6 of 9 · 24m

Composition Over Inheritance, Without Dogma

When inheritance is honest specialization, when it is implementation theft, and how composition plus dependency injection produces code you can test and replace.

'Composition over inheritance' is good advice that people repeat without knowing what problem it solves. Inheritance couples a child to implementation details of its parent: method resolution order, protected attributes, constructor timing, and overrides that silently change parent behavior. Composition couples an object only to the interface it actually uses. That difference is why composition survives refactors.

Inheritance is an 'is-a' claim, not reuse

class RetryPolicy:
    def delays(self):
        yield 0.1
        yield 0.5

class ExponentialRetryPolicy(RetryPolicy):
    def delays(self):
        yield 0.1
        yield 0.2
        yield 0.4

This is legitimate inheritance: the child preserves the parent contract and specializes behavior. By contrast, inheriting from dict to add one convenience method is implementation borrowing. You now inherit mutation methods, equality semantics, subclassing surprises, and internal representation details you never wanted.

Composition makes dependencies visible

class ReportService:
    def __init__(self, store, clock):
        self.store = store
        self.clock = clock

    def monthly_report(self, month):
        rows = self.store.rows_for(month)
        return build_report(rows, generated_at=self.clock.now())

ReportService does not care whether store is SQLAlchemy, an in-memory fake, or a protocol implementation backed by HTTP. The dependency is visible in the constructor, replaceable in tests, and narrow in scope. Inheritance would have hidden those seams inside method overrides.

Mixins are controlled inheritance

Mixins can be fine when they are small, orthogonal, and cooperate explicitly. A JsonSerializableMixin that adds to_json based on public attributes is understandable. A mixin that expects self.connection, self.logger, and _before_save to exist is a secret base class. Cooperative multiple inheritance with super() only works when every class in the chain participates correctly; one noncooperative class breaks the chain.

When inheritance is the right tool

Use inheritance for frameworks that are designed around it: Django class-based views, unittest TestCase, exception hierarchies, AST visitors. Those frameworks document extension points and own the contract. The failure mode is inventing deep taxonomies inside application code. Two levels is often already suspicious; four means the design is serving the diagram, not the change requests.

Field notes

  • If you override a method only to call the parent and add one line, ask whether a hook, callback, or composed object is cleaner.
  • Constructors should take dependencies, not construct everything internally; hidden construction is hidden coupling.
  • Favor delegation: expose narrow methods that forward to collaborators instead of leaking the collaborator itself.
  • Refactoring inheritance to composition usually reveals duplicated behavior — that duplication is information about the real abstraction.

Was this lesson helpful?

Sign in to rate