Python Glossary Русский

all Python 3abcterm

abc

module

Abstract base classes: declare an interface that subclasses are required to implement.

Syntax

from abc import ABC, abstractmethod

Use it when you need a contract: a class with @abstractmethod cannot be instantiated until a subclass implements every abstract method. Ready-made container abstractions (Iterable, Mapping) live in collections.abc, not here. A common mistake is forgetting to inherit from ABC: without the ABCMeta metaclass, @abstractmethod forbids nothing.

Examples

from abc import ABC, abstractmethod
class Shape(ABC):
    @abstractmethod
    def area(self): ...
class Square(Shape):
    def area(self): return 4
print(Square().area())  # → 4
Shape()  # → TypeError — абстрактный класс не создать

See also

Open in the full glossarydocs.python.orgReport an inaccuracy