Python Glossary Русский

Python 3.4+abcterm

abc.ABC

abstract classes

A helper base class with ABCMeta as its metaclass: subclassing it makes a class abstract (it cannot be instantiated while abstract methods remain).

Syntax

class Base(abc.ABC): ...

The abstractness check happens at instantiation time, not at class definition: a subclass that forgets to override an abstract method fails with TypeError only when you call its constructor. Inheriting from ABC by itself forbids nothing — a class with no abstract methods left over instantiates fine. When you want "does this object have the right methods" without an inheritance link, reach for typing.Protocol and structural typing instead.

Examples

import abc
class Base(abc.ABC):
    @abc.abstractmethod
    def area(self): ...
try:
    Base()
except TypeError:
    print('нельзя создать абстрактный')   # → нельзя создать абстрактный

See also

Open in the full glossarydocs.python.orgReport an inaccuracy