PYTHON Contents

Inheritance Basics (Use Carefully)

Use inheritance sparingly: keep hierarchies shallow, prefer stable interfaces, and avoid fragile base class problems.

On this page

Inheritance Trade-offs

Inheritance can reduce duplication but often increases coupling. In production, deep hierarchies make changes risky and debugging slow.

Keep It Shallow

class BaseClient:
    def request(self, path: str) -> str:
        return "base"

class ApiClient(BaseClient):
    def request(self, path: str) -> str:
        return super().request(path)

Operational Checklist

  • Prefer composition unless you have a strong "is-a" relationship.
  • Do not override methods in ways that break base assumptions.
  • Keep inheritance depth low (1–2 levels when possible).

Failure Modes

  • Fragile base class: base changes unexpectedly break subclasses.
  • Leaky abstractions: subclasses depend on internal base behavior.