abc
The Python abc module provides infrastructure for defining abstract base classes (ABCs). It allows you to create classes that define a set of methods that must be created within any subclasses built from the abstract base class.
An ABC can also be used to register virtual subclasses, which can be checked using the isinstance() or issubclass() functions.
Here’s a quick example:
>>> from abc import ABC, abstractmethod
>>> class BaseABC(ABC):
... @abstractmethod
... def method(self):
... pass
...
>>> class ConcreteClass(BaseABC):
... def method(self):
... print("Implementation of method")
...
>>> obj = ConcreteClass()
>>> obj.method()
Implementation of method
Key Features
- Defining abstract base classes using
ABCorABCMeta - Declaring abstract methods and properties with the
@abstractmethoddecorator - Preventing instantiation until all abstract methods are implemented
- Registering virtual subclasses with
ABCMeta.register()without using inheritance - Customizing
issubclass()checks via the.__subclasshook__()magic method - Supporting mixin-style ABCs in multiple inheritance hierarchies
- Exposing ABC cache/version helpers via
.get_cache_token() - Updating abstract method sets after class creation with
.update_abstractmethods()
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
abc.ABC |
Class | Provides a helper base class for defining abstract base classes |
abc.abstractmethod() |
Decorator | Marks a method as abstract |
abc.ABCMeta |
Metaclass | Defines the metaclass for defining abstract base classes |
abc.ABCMeta.register() |
Method | Registers a class as a virtual subclass |
builtins.isinstance() |
Built-in Function | Checks whether an object is an instance of a class or of a tuple of classes |
builtins.issubclass() |
Built-in Function | Checks whether a class is a subclass of a class or of a tuple of classes |
Examples
Define an abstract base class:
>>> from abc import ABC, abstractmethod
>>> class Vehicle(ABC):
... @abstractmethod
... def drive(self):
... pass
Register a virtual subclass:
>>> from abc import ABC, abstractmethod
>>> class Car:
... def drive(self):
... print("Car is driving")
...
>>> Vehicle.register(Car)
>>> issubclass(Car, Vehicle)
True
>>> isinstance(Car(), Vehicle)
True
Common Use Cases
The most common tasks for abc include:
- Defining required APIs that concrete subclasses must implement
- Preventing incomplete implementations from being instantiated
- Building framework base classes and plugin interfaces with enforced override points
- Validating objects and classes at runtime with
isinstance()andissubclass() - Registering third-party classes as virtual subclasses when inheritance isn’t practical
- Implementing structural interface checks via
.__subclasshook__() - Creating mixins that provide shared behavior while still requiring concrete methods
Real-World Example
Suppose you’re building a plugin system where each plugin must implement a .run() method. You can use an ABC to enforce this requirement:
>>> from abc import ABC, abstractmethod
>>> class Plugin(ABC):
... @abstractmethod
... def run(self):
... pass
...
>>> class CustomPlugin(Plugin):
... def run(self):
... print("Running custom plugin...")
...
>>> def execute_plugin(plugin):
... plugin.run()
...
>>> plugin = CustomPlugin()
>>> execute_plugin(plugin)
Running custom plugin...
In this example, the abc module ensures that any class claiming to be a Plugin must implement the .run() method, providing a consistent interface for executing plugins.