abc – Abstract Base Classes
Purpose: | Define and use abstract base classes for API checks in your code. |
---|---|
Available In: | 2.6 |
Why use Abstract Base Classes?
Abstract base classes are a form of interface checking more strict than individual hasattr()checks for particular methods. By defining an abstract base class, you can define a common API for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins to an application, but can also aid you when working on a large team or with a large code-base where keeping all classes in your head at the same time is difficult or not possible.
How ABCs Work
abc works by marking methods of the base class as abstract, and then registering concrete classes as implementations of the abstract base. If your code requires a particular API, you can use issubclass() or isinstance() to check an object against the abstract class.
Let’s start by defining an abstract base class to represent the API of a set of plugins for saving and loading data.
Registering a Concrete Class
There are two ways to indicate that a concrete class implements an abstract: register the class with the abc or subclass directly from the abc.
In this example the PluginImplementation is not derived from PluginBase, but is registered as implementing the PluginBase API.
Implementation Through Subclassing
By subclassing directly from the base, we can avoid the need to register the class explicitly.
In this case the normal Python class management is used to recognizePluginImplementation as implementing the abstract PluginBase.
A side-effect of using direct subclassing is it is possible to find all of the implementations of your plugin by asking the base class for the list of known classes derived from it (this is not an abc feature, all classes can do this).
Notice that even though abc_register is imported, RegisteredImplementation is not among the list of subclasses because it is not actually derived from the base.
Dr. André Roberge has described using this capability to discover plugins by importing all of the modules in a directory dynamically and then looking at the subclass list to find the implementation classes.
Incomplete Implementations
Another benefit of subclassing directly from your abstract base class is that the subclass cannot be instantiated unless it fully implements the abstract portion of the API. This can keep half-baked implementations from triggering unexpected errors at runtime.
Concrete Methods in ABCs
Although a concrete class must provide an implementation of an abstract methods, the abstract base class can also provide an implementation that can be invoked via super(). This lets you re-use common logic by placing it in the base class, but force subclasses to provide an overriding method with (potentially) custom logic.
Since ABCWithConcreteImplementation is an abstract base class, it isn’t possible to instantiate it to use it directly. Subclasses must provide an override for retrieve_values(), and in this case the concrete class massages the data before returning it at all.
Abstract Properties
If your API specification includes attributes in addition to methods, you can require the attributes in concrete classes by defining them with @abstractproperty.
The Base class in the example cannot be instantiated because it has only an abstract version of the property getter method.
You can also define abstract read/write properties.
Notice that the concrete property must be defined the same way as the abstract property. Trying to override a read/write property in PartialImplementation with one that is read-only does not work.
To use the decorator syntax does with read/write abstract properties, the methods to get and set the value should be named the same.
Notice that both methods in the Base and Implementation classes are named value(), although they have different signatures.
Collection Types
The collections module defines several abstract base classes related to container (and containable) types.
General container classes:
- Container
- Sized
Iterator and Sequence classes:
- Iterable
- Iterator
- Sequence
- MutableSequence
Unique values:
- Hashable
- Set
- MutableSet
Mappings:
- Mapping
- MutableMapping
- MappingView
- KeysView
- ItemsView
- ValuesView
Miscelaneous:
- Callable
In addition to serving as detailed real-world examples of abstract base classes, Python’s built-in types are automatically registered to these classes when you import collections. This means you can safely use isinstance() to check parameters in your code to ensure that they support the API you need. The base classes can also be used to define your own collection types, since many of them provide concrete implementations of the internals and only need a few methods overridden. Refer to the standard library docs for collections for more details.
See also:
- abc
- The standard library documentation for this module.
- PEP 3119
- Introducing Abstract Base Classes
- collections
- The collections module includes abstract base classes for several collection types.
- collections
- The standard library documentation for collections.
- PEP 3141
- A Type Hierarchy for Numbers
- Wikipedia: Strategy Pattern
- Description and examples of the strategy pattern.
- Plugins and monkeypatching
- PyCon 2009 presentation by Dr. André Roberge
Much like a laser cutter, EDM does not need mechanical force within the removing course of, which is why it’s thought of a non-traditional methodology, comparability to|compared to} processes that require chopping tools. The program usually accommodates G-codes that describe CNC machining the precise contours of the part, nicely as|in addition to} the exact timing that the torch should be on or off. The elements programs are designed by software program known as post-processors.