Python 3.3+

from abc import ABCMeta, abstractmethod


class A(metaclass=ABCMeta):
    def __init__(self):
        # ...
        pass

    @property
    @abstractmethod
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass

Failure to declare a or b in the derived class B will raise a TypeError such as:

TypeError: Can't instantiate abstract class B with abstract methods a

Python 2.7

There is an @abstractproperty decorator for this:

from abc import ABCMeta, abstractmethod, abstractproperty


class A:
    __metaclass__ = ABCMeta

    def __init__(self):
        # ...
        pass

    @abstractproperty
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass
Answer from Wtower on Stack Overflow
Top answer
1 of 13
183

Python 3.3+

from abc import ABCMeta, abstractmethod


class A(metaclass=ABCMeta):
    def __init__(self):
        # ...
        pass

    @property
    @abstractmethod
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass

Failure to declare a or b in the derived class B will raise a TypeError such as:

TypeError: Can't instantiate abstract class B with abstract methods a

Python 2.7

There is an @abstractproperty decorator for this:

from abc import ABCMeta, abstractmethod, abstractproperty


class A:
    __metaclass__ = ABCMeta

    def __init__(self):
        # ...
        pass

    @abstractproperty
    def a(self):
        pass

    @abstractmethod
    def b(self):
        pass


class B(A):
    a = 1

    def b(self):
        pass
2 of 13
127

Since this question was originally asked, python has changed how abstract classes are implemented. I have used a slightly different approach using the abc.ABC formalism in python 3.6. Here I define the constant as a property which must be defined in each subclass.

from abc import ABC, abstractmethod


class Base(ABC):

    @classmethod
    @property
    @abstractmethod
    def CONSTANT(cls):
        raise NotImplementedError

    def print_constant(self):
        print(self.CONSTANT)


class Derived(Base):
    CONSTANT = 42

This forces the derived class to define the constant, or else a TypeError exception will be raised when you try to instantiate the subclass. When you want to use the constant for any functionality implemented in the abstract class, you must access the subclass constant by type(self).CONSTANT instead of just CONSTANT, since the value is undefined in the base class.

There are other ways to implement this, but I like this syntax as it seems to me the most plain and obvious for the reader.

The previous answers all touched useful points, but I feel the accepted answer does not directly answer the question because

  • The question asks for implementation in an abstract class, but the accepted answer does not follow the abstract formalism.
  • The question asks that implementation is enforced. I would argue that enforcement is stricter in this answer because it causes a runtime error when the subclass is instantiated if CONSTANT is not defined. The accepted answer allows the object to be instantiated and only throws an error when CONSTANT is accessed, making the enforcement less strict.

This is not to fault the original answers. Major changes to the abstract class syntax have occurred since they were posted, which in this case allow a neater and more functional implementation.

🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
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:
Discussions

Provide a canonical way to declare an abstract class variable - Ideas - Discussions on Python.org
There’s a recent help post of Abstract variables in abc that asks about how an “abstract variable” can be declared such that it is required for a subclass to override the variable, to which @drmason13 replied: Although this approach of abusing an abstract property as an abstract variable ... More on discuss.python.org
🌐 discuss.python.org
10
October 28, 2024
Allow overriding (abstract) properties with fields - Ideas - Discussions on Python.org
I often find myself wanting to do someting like this: from abc import abstractmethod from dataclasses import dataclass class HasLength: @property @abstractmethod def len(self) -> int: ... def __len__(s… More on discuss.python.org
🌐 discuss.python.org
3
November 20, 2022
[abc] Add abstract attributes via `abstract` type-hint - Ideas - Discussions on Python.org
Feature or enhancement Add a special generic type hint abstract, that allows specifying that subclasses must implement an attribute. from abc import ABC, abstract class Foo(ABC): myattr: abstract[int] # 100 upvotes) How to create abstract properties in python a... More on discuss.python.org
🌐 discuss.python.org
8
April 24, 2023
How to define an abstract property?
I'd like to define a base class that specifies a set of fields that children of this class must define. I expected that I'd be able to do this via abstract base classes, but that's not ... More on github.com
🌐 github.com
3
14
February 26, 2021
🌐
Python.org
discuss.python.org › ideas
Provide a canonical way to declare an abstract class variable - Ideas - Discussions on Python.org
October 28, 2024 - There’s a recent help post of Abstract variables in abc that asks about how an “abstract variable” can be declared such that it is required for a subclass to override the variable, to which @drmason13 replied: Although this approach of abusing an abstract property as an abstract variable ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
An Abstract Base Class (ABC) defines methods that must be implemented by its subclasses, ensuring that the subclasses follow a consistent structure. ABCs allow you to define common interfaces that various subclasses can implement while enforcing ...
Published   September 3, 2025
🌐
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - You create an abstract class called Shape that says every shape must have an area() method. But Shape doesn’t define how area() works—because the formula depends on the type of shape. Each specific shape (like a Circle or Rectangle) inherits from Shape and provides its own version of area(). If you're looking to learn more about key Python concepts, you can enroll in our Intermediate Object-Oriented Programming in Python course.
🌐
Python.org
discuss.python.org › ideas
Allow overriding (abstract) properties with fields - Ideas - Discussions on Python.org
November 20, 2022 - I often find myself wanting to do someting like this: from abc import abstractmethod from dataclasses import dataclass class HasLength: @property @abstractmethod def len(self) -> int: ... def __len__(self) -> int: return self.len @dataclass class MyClass(HasLength): len: int m = MyClass(3) # AttributeError: can't set attribute 'len' len(m) but trying to override a property in a base class with a dataclass field causes an AttributeError.
Find elsewhere
🌐
Python.org
discuss.python.org › ideas
[abc] Add abstract attributes via `abstract` type-hint - Ideas - Discussions on Python.org
April 24, 2023 - Feature or enhancement Add a special generic type hint abstract, that allows specifying that subclasses must implement an attribute. from abc import ABC, abstract class Foo(ABC): myattr: abstract[int] # <- subcla…
🌐
Scaler
scaler.com › home › topics › abstract class in python
Abstract Class in Python - Scaler Topics
April 9, 2024 - Abstraction is implemented using the abstract classes. An abstract class in Python is typically created to declare a set of methods that must be created in any child class built on top of this abstract class.
🌐
Python Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
Our example implemented a case of simple inheritance which has nothing to do with an abstract class. In fact, Python on its own doesn't provide abstract classes. Yet, Python comes with a module which provides the infrastructure for defining Abstract Base Classes (ABCs).
🌐
The Teclado Blog
blog.teclado.com › python-abc-abstract-base-classes
How to Write Cleaner Python Code Using Abstract Classes
October 26, 2022 - 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.
🌐
CodeFatherTech
codefather.tech › home › blog › create an abstract class in python: a step-by-step guide
Create an Abstract Class in Python: A Step-By-Step Guide
December 8, 2024 - An Abstract class is a template that enforces a common interface and forces classes that inherit from it to implement a set of methods and properties. The Python abc module provides the functionalities to define and use abstract classes.
🌐
Earthly
earthly.dev › blog › abstract-base-classes-python
Abstract Base Classes in Python - Earthly Blog
July 19, 2023 - However, regular classes in Python have limitations that can make it challenging to create modular and maintainable code. One limitation of regular classes is that they cannot enforce the implementation of certain methods or attributes, making it difficult for objects of different classes to be used interchangeably in code. Additionally, regular classes cannot be used for type checking at runtime, which can lead to errors in code. Abstract Base Classes (ABCs) offer a solution to these limitations by allowing us to define a set of common methods and attributes that must be implemented by any class that inherits from the ABC.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
June 1, 2025 - Abstract base classes help organize ... code easier to extend. ... In Python, abstract properties allow you to define properties in an abstract class that any subclass must implement....
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to use abstract classes in python
How to Use Abstract Classes in Python | Towards Data Science
January 21, 2025 - What is an Abstract Class? An abstract class is a class, but not one you can create objects from directly. Its purpose is to define how other classes should look like, i.e. what methods and properties they are expected to have.
🌐
Medium
martinxpn.medium.com › abstract-classes-in-python-51-100-days-of-python-94a80879ca6f
Abstract Classes in Python (51/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - In this code, we import the ABC and abstractmethod modules from the abc module, which stands for Abstract Base Classes. We create a class called MyClass that inherits from ABC, and we decorate the some_method with the @abstractmethod decorator. This tells Python that some_method is an abstract method that needs to be implemented by the derived classes.
🌐
Python Tutorial
pythontutorial.net › home › python oop › python abstract classes
Python Abstract Class
March 31, 2025 - from abc import ABC, abstractmethod class AbstractClassName(ABC): @abstractmethod def abstract_method_name(self): pass Code language: Python (python)