Using collections.abc

Abstract base classes (ABCs) are like small building blocks for creating a higher level of abstraction. They allow you to implement really usable interfaces, but are very generic and designed to handle a lot more than this single design pattern. You can unleash your creativity and do magical things, but building something generic and really usable may require a lot of work. Work that may never pay off.

This is the reason custom abstract base classes are not used so often. Despite that, the collections.abc module provides a lot of predefined ABCs that allow for compatibility of types with common Python interfaces. With the base classes provided in this module, you can check, for example, whether a given object is callable, mapping, or whether it supports iteration. Using them with the isinstance() function is way better than comparing against the base Python types. You should definitely know how to use these base classes even if you don't want to define your own custom interfaces with ABCMeta.

The most common abstract base classes from the collections.abc that you will use from time to time are as follows:

A full list of the available abstract base classes from the collections.abc module is available in the official Python documentation (refer to https://docs.python.org/3/library/collections.abc.html).