abc — Abstract Base Classes¶
This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also PEP 3141 and the numbers module regarding a type hierarchy for numbers based on ABCs.)
The collections module has some concrete classes that derive from ABCs; these can, of course, be further derived. In addition, the collections.abc submodule has some ABCs that can be used to test whether a class or instance provides a particular interface, for example, if it is hashable or if it is a mapping.
This module provides the metaclass ABCMeta for defining ABCs and a helper class ABC to alternatively define ABCs through inheritance:
A helper class that has ABCMeta as its metaclass. With this class, an abstract base class can be created by simply deriving from ABC avoiding sometimes confusing metaclass usage, for example:
Note that the type of ABC is still ABCMeta , therefore inheriting from ABC requires the usual precautions regarding metaclass usage, as multiple inheritance may lead to metaclass conflicts. One may also define an abstract base class by passing the metaclass keyword and using ABCMeta directly, for example:
New in version 3.4.
Metaclass for defining Abstract Base Classes (ABCs).
Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as “virtual subclasses” – these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won’t show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super() ). 1
Classes created with a metaclass of ABCMeta have the following method:
Register subclass as a “virtual subclass” of this ABC. For example:
Changed in version 3.3: Returns the registered subclass, to allow usage as a class decorator.
Changed in version 3.4: To detect calls to register() , you can use the get_cache_token() function.
You can also override this method in an abstract base class:
(Must be defined as a class method.)
Check whether subclass is considered a subclass of this ABC. This means that you can customize the behavior of issubclass further without the need to call register() on every class you want to consider a subclass of the ABC. (This class method is called from the __subclasscheck__() method of the ABC.)
This method should return True , False or NotImplemented . If it returns True , the subclass is considered a subclass of this ABC. If it returns False , the subclass is not considered a subclass of this ABC, even if it would normally be one. If it returns NotImplemented , the subclass check is continued with the usual mechanism.
For a demonstration of these concepts, look at this example ABC definition:
The ABC MyIterable defines the standard iterable method, __iter__() , as an abstract method. The implementation given here can still be called from subclasses. The get_iterator() method is also part of the MyIterable abstract base class, but it does not have to be overridden in non-abstract derived classes.
The __subclasshook__() class method defined here says that any class that has an __iter__() method in its __dict__ (or in that of one of its base classes, accessed via the __mro__ list) is considered a MyIterable too.
Finally, the last line makes Foo a virtual subclass of MyIterable , even though it does not define an __iter__() method (it uses the old-style iterable protocol, defined in terms of __len__() and __getitem__() ). Note that this will not make get_iterator available as a method of Foo , so it is provided separately.
The abc module also provides the following decorator:
A decorator indicating abstract methods.
Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden. The abstract methods can be called using any of the normal ‘super’ call mechanisms. abstractmethod() may be used to declare abstract methods for properties and descriptors.
Dynamically adding abstract methods to a class, or attempting to modify the abstraction status of a method or class once it is created, are only supported using the update_abstractmethods() function. The abstractmethod() only affects subclasses derived using regular inheritance; “virtual subclasses” registered with the ABC’s register() method are not affected.
When abstractmethod() is applied in combination with other method descriptors, it should be applied as the innermost decorator, as shown in the following usage examples:
In order to correctly interoperate with the abstract base class machinery, the descriptor must identify itself as abstract using __isabstractmethod__ . In general, this attribute should be True if any of the methods used to compose the descriptor are abstract. For example, Python’s built-in property does the equivalent of:
Unlike Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the super() mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance.
The abc module also supports the following legacy decorators:
New in version 3.2.
Deprecated since version 3.3: It is now possible to use classmethod with abstractmethod() , making this decorator redundant.
A subclass of the built-in classmethod() , indicating an abstract classmethod. Otherwise it is similar to abstractmethod() .
This special case is deprecated, as the classmethod() decorator is now correctly identified as abstract when applied to an abstract method:
New in version 3.2.
Deprecated since version 3.3: It is now possible to use staticmethod with abstractmethod() , making this decorator redundant.
A subclass of the built-in staticmethod() , indicating an abstract staticmethod. Otherwise it is similar to abstractmethod() .
This special case is deprecated, as the staticmethod() decorator is now correctly identified as abstract when applied to an abstract method:
Deprecated since version 3.3: It is now possible to use property , property.getter() , property.setter() and property.deleter() with abstractmethod() , making this decorator redundant.
A subclass of the built-in property() , indicating an abstract property.
This special case is deprecated, as the property() decorator is now correctly identified as abstract when applied to an abstract method:
The above example defines a read-only property; you can also define a read-write abstract property by appropriately marking one or more of the underlying methods as abstract:
If only some components are abstract, only those components need to be updated to create a concrete property in a subclass:
The abc module also provides the following functions:
Returns the current abstract base class cache token.
The token is an opaque object (that supports equality testing) identifying the current version of the abstract base class cache for virtual subclasses. The token changes with every call to ABCMeta.register() on any ABC.
New in version 3.4.
A function to recalculate an abstract class’s abstraction status. This function should be called if a class’s abstract methods have been implemented or changed after it was created. Usually, this function should be called from within a class decorator.
Returns cls, to allow usage as a class decorator.
If cls is not an instance of ABCMeta , does nothing.
This function assumes that cls’s superclasses are already updated. It does not update any subclasses.
New in version 3.10.
C++ programmers should note that Python’s virtual base class concept is not the same as C++’s.
ABC in Python (Abstract Base Class)
![]()
An Abstract class is one of important concept in object oriented programming(oops). It is like blueprint for other classes.
Where we use abstract?
For larger projects, it is impossible to remember the class details, and also the reusability of code can increase the bug. Therefore, it plays a crucial role in our projects.
By default, Python does not provide abstract classes. The ‘abc’ module in the Python library provides the infrastructure for defining custom abstract base classes.
Abstract class cannot be instantiated in python. An Abstract method can be call by its subclasses.
The above module provides us the following,
- ABCMeta
- ABC ( helper class )
these are keywords used to create an abstract class.
ABCMeta:
It is a metaclass for defining the abstract class.
ABC:
It is a helper class. we can use it in the area where we want to avoid the confusion of metaclass usage.
Both the steps are similar.
Note: The type of ABC ( type(ABC) ) is still ABCMeta. therefore inheriting from ABC requires the usual precautions regarding metaclass usage, as multiple inheritance may lead to metaclass conflicts.
register():
We can make a concrete subclass or built-in subclass as a virtual subclass using the register. But it is not visible in the Method Resolution Order(MRO). Its method is not even callable using super().
@abstractmethod:
It is used to make the method of the abstract class as an abstract method.
The abstract methods can be called using any of the normal ‘super’ call mechanisms. abstractmethod() may be used to declare abstract methods for properties and descriptors.
Dynamically adding abstract methods to a class, or attempting to modify the abstraction status of a method or class once it is created, are not supported. The abstractmethod() only affects subclasses derived using regular inheritance; “virtual subclasses” registered with the ABC’s register() method are not affected.
There are other decorators (@abstractclassmethod, @abstractstaticmethod ) and also some magic methods available. We can discuss about that further.
How to Write Cleaner Python Code Using Abstract Classes

What are Abstract Classes? Why are they useful? When should you use them? Let me give you a few examples and explanations! By the end of this post, you’ll have a firm understanding of ABCs in Python, and how to add them to your programs.
Our code without abstract classes
I believe the best way to learn is with an example, so let’s take a look at the following code:
Our job is to feed all the animals using a Python script. One way to do that would be:
This would work. But imagine how much time would it take to do this for each animal in a large zoo, repeating the same process and code hundreds of times. That would also make the code harder to maintain.
Currently our program’s structure looks something like this:

We want to optimize the process, so we could come up with a solution like this one:
The problem is that every class has a different method name, when feeding a lion it’s give_food() , when feeding a panda it’s feed_animal() and it’s feed_snake() for a snake.
This code is a mess because methods that do the same thing should be named the same.
If we could only force our classes to implement the same method names.
Introducing Abstract classes
It turns out that the Abstract class is what we need. Essentially it forces its subclasses to implement all of its abstract methods. It is a class that represents what its subclasses look like.
A better structure could look like like this ( Animal is an Abstract class):

By introducing an abstract class ( Animal ), every class that inherits from Animal must implement abstract methods from Animal , which in our case is the method feed()
Let’s take a look at the code:
When defining an abstract class we need to inherit from the Abstract Base Class — ABC .
To define an abstract method in the abstract class, we have to use a decorator: @abstractmethod . The built-in abc module contains both of these.
If you inherit from the Animal class but don’t implement the abstract methods, you’ll get an error:
If we try to instantiate the class (e.g. po = Panda() ) it will throw a TypeError since we can’t instantiate Panda without an abstract method feed() .
Keeping that in mind, we need to make our animals (this time correctly):
And lastly, this is all the code we need in order to create and feed our animals:
Writing abstract methods with parameters
What happens when an abstract method has parameters? When the subclass implements the method, it must contain all the parameters as well. The subclass’ implementation can also add extra parameters if required.
Running the above code will print out:
We could also use default arguments, you can read about those here.
Writing (abstract) properties
We may also want to create abstract properties and force our subclass to implement those properties. This could be done by using @property decorator along with @absctractmethod .
Since animals often have different diets, we’ll need to define a diet in our animal classes. Since all the animals are inheriting from Animal , we can define diet to be an abstract property. Besides diet , we’ll make food_eaten property and it’s setter will check if we are trying to feed the animal something that’s not on it’s diet .
Take a look at the code of Animal , Lion and Snake :
We can create two objects, set the food that we’re going to feed them and then call the feed() method:
That will print out:
If we try to feed an animal something that it doesn’t eat:
The setter will raise a ValueError :
A word on abstract Meta class
You may have come across metaclasses when learning about abstract classes.
A class defines how an instance of the class behaves (e.g. Animal describes how Lion will behave). On the other hand a metaclass defines how a class behaves ( ABCMeta describes how every ABC class will behave). A class is an instance of a metaclass.
The abc module comes with a metaclass ABCMeta . back in the days we had to use it to define metaclasses with metaclass=abc.ABCMeta .
Nowadays, just inheriting from ABC does the same thing—so you don’t have to worry about metaclasses at all!
Summary
In this blog post we described the basics of Python’s abstract classes. They are especially useful when working in a team with other developers and parts of the project are developed in parallel.
Here are some key takeaways:
- Abstract classes make sure that derived classes implement methods and properties defined in the abstract base class.
- Abstract base class can’t be instantiated.
- We use @abstractmethod to define a method in the abstract base class and combination of @property and @abstractmethod in order to define an abstract property.
I hope you learnt something new today! If you’re looking to upgrade your Python skills even further, check out our Complete Python Course.

Petar Marković
Hello! I'm a teaching assistant and a content writer at Teclado. I'm at the beginning of my career and I am currently learning and working with Python.
Абстрактные классы и интерфейсы в Питоне
Абстрактные базовые классы и интерфейсы — близкие по назначению и смыслу сущности. Как первые, так и вторые представляют собой своеобразный способ документирования кода и помогают ограничить (decouple) взаимодействие отдельных абстракций в программе (классов).
Питон — очень гибкий язык. Одна из граней этой гибкости — возможности, предоставляемые метапрограммированием. И хотя в ядре языка абстрактные классы и интерфейсы не представлены, первые были реализованы в стандартном модуле abc, вторые — в проекте Zope (модуль zope.interfaces).
Нет смысла одновременно использовать и то и другое, и поэтому каждый программист должен определить для себя, какой инструмент использовать при проектировании приложений.
2 Абстрактные базовые классы (abс)
Начиная с версии языка 2.6 в стандартную библиотеку включается модуль abc, добавляющий в язык абстрактные базовые классы (далее АБК).
АБК позволяют определить класс, указав при этом, какие методы или свойства обязательно переопределить в классах-наследниках:
Таким образом, если мы хотим использовать в коде объект, обладающий возможностью перемещения и определенной скоростью, то следует использовать класс Movable в качестве одного из базовых классов.
Наличие необходимых методов и атрибутов объекта теперь гарантируется наличием АБК среди предков класса:
Видно, что понятие АБК хорошо вписывается в иерархию наследования классов, использовать их легко, а реализация, если заглянуть в исходный код модуля abc, очень проста. Абстрактные классы используются в стандартных модулях collections и number, задавая необходимые для определения методы пользовательских
классов-наследников.
Подробности и соображения по поводу использования АБК можно найти в PEP 3119
(http://www.python.org/dev/peps/pep-3119/).
3 Интерфейсы (zope.interfaces)
Реализация проекта Zope в работе над Zope3 решила сделать акцент на компонентной архитектуре; фреймворк превратился в набор практически независимых компонент. Клей, соединяющий компоненты — интерфейсы и основывающиеся на них адаптеры.
Модуль zope.interfaces — результат этой работы.
В простейшем случае использвание интерфейсов напоминает примерение АБК:
В интерфейсе декларативно показывается, какие атрибуты и методы должны быть у объекта. Причем класс реализует (implements) интерфейс, а объект класса — предоставляет (provides). Следует обратить внимание на разницу между этими понятиями!
«Реализация» чем-либо интерфейса означает, что только «производимая» сущность будет обладать необходимыми свойствами; а «предоставление» интерфейса говорит о конкретных возможностях оцениваемой сущности. Соответственно, в Питоне классы, кстати, могут как реализовывать, так и предоставлять интерфейс.
На самом деле декларация implement(IVehicle) — условность; просто обещание, что данный класс и его объекты ведут себя именно таким образом. Никаких реальных проверок проводиться не будет
Видно, что в простейших случаях интерфейсы только усложняют код, как, впрочем, и АБК
Компонентная архитектура Zope включает еще одно важное понятие — адаптеры. Вообще говоря, это простой шаблон проектирования, корректирующий один класс для использования где-то, где требуется иной комплект методов и атрибутов. Итак,
4 Адаптеры
Предположим, что имеется пара классов, Guest и Desk. Определим интерфейсы к ним, плюс класс, реализующий интерфейс Guest:
Адаптер должен учесть анонимного гостя, зарегистрировав в списке имен:
Существует реестр, который ведет учет адаптеров по интерфейсам. Благодаря ему можно получить адаптер, передав в вызов класса-интерфейса адаптируемый объект. Если адаптер не зарегистрирован, то вернется второй аргумент интерфейса:
Такую инфраструктуру удобно использовать для разделения кода на компоненты и их связывания.
Один из ярчайших примеров использования такого подхода помимо самого Zope — сетевой фреймворк Twisted, где изрядная часть архитектуры опирается на интерфейсы из zope.interfaces.
5 Вывод
При ближайшем рассмотрении оказывается, что интерфейсы и абстрактные базовые классы — разные вещи.
Абстрактные классы в основном жестко задают обязательную интерфейсную часть. Проверка объекта на соответствие интерфейсу абстрактного класса проверяется при помощи встроенной функции isinstance; класса — issubclass. Абстрактный базовый класс должен включаться в иерархию в виде базового класса либо mixin`а.
Минусом можно считать семантику проверок issubclass, isinstance, которые пересекаются с обычными классами (их иерархией наследования). На АБК не выстраивается никаких допонительных абстракций.
Интерфейсы — сущность декларативная, они не ставят никаких рамок; просто утверждается, что класс реализует, а его объект предоставляет интерфейс. Семантически утверждения implementedBy, providedBy являются более корректными. На такой простой базе удобно выстраивать компонентную архитектуру при помощи адапетров и других производных сущностей, что и делают крупные фреймворки Zope и Twisted.
Надо понимать, что использование обоих инструментов имеет смысл только при построении и использовании сравнительно крупных ООП-систем — фреймворков и библиотек, в малых программах они могут только запутать и усложнить код код лишними абстракциями.