Python:abc
lastmod 2025-04-25created 2021-02-189 edits
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.)
Example
import abc
class BaseClass(metaclass=ABCMeta):
@abc.abstractmethod
def func1(self):
pass
@abc.abstractmethod
def func2(self):
pass
abstractmethod
property abstractmethod
from abc import ABC, abstractmethod
class A(ABC):
@property
@abstractmethod
def mode(self) -> str:
pass
Decorator order when used with @override:
abstractclassmethod
Deprecated. Use the following instead:
abstractstaticmethod
Deprecated. Use the following instead:
abstractproperty
Deprecated. Use the following instead:
ABC vs Protocol
In short: if the inheritance structure is already in place and hard to control, use Protocol; if you can design from scratch, use ABC
Metaclass
Explained in detail on the Python:Metaclass page.
See also
- Python
- Abstract Base Classes
Favorite site
Pages that link here