Abstract classes using collections.abc
The abstract base classes (also known as interface classes) module is one of the most useful and most widely used examples of metaclasses in Python, as it makes it easy to ensure that a class adheres to a certain interface without a lot of manual checks. We have already seen some examples of abstract base classes in previous chapters, but now we will also look at their inner workings and some more advanced features, such as custom abstract base classes (ABCs).
Internal workings of the abstract classes
First, let’s demonstrate the usage of the regular abstract base class:
>>> import abc
>>> class AbstractClass(metaclass=abc.ABCMeta):
... @abc.abstractmethod
... def some_method(self):
... raise NotImplemented()
>>> class ConcreteClass(AbstractClass):
... pass
>>> ConcreteClass()
Traceback (most recent call last):
...
TypeError: Can't instantiate abstract class...