Skip to main content

Abstract Base Classes in Python

OOP Architecture

Abstract Base Classes

Abstract base classes (ABCs) enforce contracts and provide shared helpers.

Definition

from abc import ABC, abstractmethod

class StorageDriver(ABC):
@abstractmethod
def write(self, path: str, data: bytes) -> None: ...

@abstractmethod
def read(self, path: str) -> bytes: ...
  • Subclasses must implement abstract methods before they can be instantiated.
  • Mix in concrete helper methods to reduce duplication.

Registration

Use StorageDriver.register(SomeClass) to treat unrelated classes as virtual subclasses without inheritance.

Next up in your learning path