Skip to content
Search

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:

class B(A):
    @property
    @override
    def mode(self) -> str:
        return ""

abstractclassmethod

Deprecated. Use the following instead:

class C(ABC):
    @classmethod
    @abstractmethod
    def my_abstract_classmethod(cls, ...):
        ...

abstractstaticmethod

Deprecated. Use the following instead:

class C(ABC):
    @staticmethod
    @abstractmethod
    def my_abstract_staticmethod(...):
        ...

abstractproperty

Deprecated. Use the following instead:

class C(ABC):
    @property
    @abstractmethod
    def my_abstract_property(self):
        ...

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

author Zer0 sitemap