Since Python 3.3 a bug was fixed meaning the property() decorator is now correctly identified as abstract when applied to an abstract method.

Note: Order matters, you have to use @property above @abstractmethod

Python 3.3+: (python docs):

from abc import ABC, abstractmethod

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

Python 2: (python docs)

from abc import ABCMeta, abstractproperty

class C:
    __metaclass__ = ABCMeta

    @abstractproperty
    def my_abstract_property(self):
        ...
Answer from James on Stack Overflow
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
The above example defines a read-only property; you can also define a read-write abstract property by appropriately marking one or more of the underlying methods as abstract: class C(ABC): @property def x(self): ...
🌐
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - To guarantee that every subclass provides its implementation, abstract properties can be specified using the @property decorator in combination with @abstractmethod. When creating classes that need read-only or computed attributes that are essential to the class's operation, this method is ...
🌐
The Teclado Blog
blog.teclado.com › python-abc-abstract-base-classes
How to Write Cleaner Python Code Using Abstract Classes
October 26, 2022 - Besides diet, we'll make food_eaten property and it's setter will check if we are trying to feed the animal something that's not on it's diet. Take a look at the code of Animal, Lion and Snake: from abc import ABC, abstractmethod class Animal(ABC): @property def food_eaten(self): return self._food @food_eaten.setter def food_eaten(self, food): if food in self.diet: self._food = food else: raise ValueError(f"You can't feed this animal with {food}.") @property @abstractmethod def diet(self): pass @abstractmethod def feed(self, time): pass class Lion(Animal): @property def diet(self): return ["antelope", "cheetah", "buffaloe"] def feed(self, time): print(f"Feeding a lion with {self._food} meat!
🌐
Machine Learning Plus
machinelearningplus.com › python › python-abcs-the-complete-guide-to-abstract-base-classes
Python ABCs- The Complete Guide to Abstract Base Classes – Machine Learning Plus
Let’s create a MediaFile abstract base class with two abstract properties file_format and media_type, plus an abstract method play() and a concrete method get_info(). ... class MediaFile(ABC): def __init__(self, filename): self.filename = filename @property @abstractmethod def file_format(self): pass @property @abstractmethod def media_type(self): pass @abstractmethod def play(self): pass def get_info(self): return f"{self.filename} ({self.file_format})" class VideoFile(MediaFile): @property def file_format(self): return "MP4" @property def media_type(self): return "Video" def play(self): re
🌐
Python
docs.python.org › 3.8 › library › abc.html
abc — Abstract Base Classes — Python 3.8.20 documentation
This special case is deprecated, as the property() decorator is now correctly identified as abstract when applied to an abstract method: class C(ABC): @property @abstractmethod def my_abstract_property(self): ...
Find elsewhere
🌐
Python
docs.python.org › 3.10 › library › abc.html
abc — Abstract Base Classes — Python 3.10.18 documentation
The above example defines a read-only property; you can also define a read-write abstract property by appropriately marking one or more of the underlying methods as abstract: class C(ABC): @property def x(self): ...
🌐
Geek Python
geekpython.in › abc-in-python
Python's ABC: Understanding the Basics of Abstract Base Classes
October 29, 2023 - Just as the abc module allows us to define abstract methods using the @abstractmethod decorator, it also allows us to define abstract properties using the @abstractproperty decorator.
🌐
Delft Stack
delftstack.com › home › howto › python › python abstract property
Python Abstract Property | Delft Stack
February 12, 2024 - When one tries to make an object of that class to access the methods, Python will give an error. For example, let’s make the methods of a subclass abstract. See the code below. # Class Code from abc import ABC, abstractmethod class Bike(ABC): @property @abstractmethod def mileage(self): pass class Honda(Bike): @abstractmethod def mileage(self): print("The mileage is 20kmph") def mileage2(self): print("The mileage is 200 kmph") # Main Code b = Honda() b.mileage2()
🌐
GitHub
github.com › python › cpython › blob › main › Lib › abc.py
cpython/Lib/abc.py at main · python/cpython
Deprecated, use 'property' with 'abstractmethod' instead: · class C(ABC): @property · @abstractmethod · def my_abstract_property(self): ... · """ · __isabstractmethod__ = True ·
Author   python
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
September 12, 2024 - In Python, abstract properties allow you to define properties in an abstract class that any subclass must implement. Like abstract methods, abstract properties are defined without implementation in the base class, but they must be given concrete ...
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.

🌐
GitHub
github.com › python › mypy › issues › 8532
Access abstract class property from class method · Issue #8532 · python/mypy
January 15, 2020 - When accessing a class property from a class method mypy does not respect the property decorator. Steps to reproduce: class Example: @property @classmethod def name(cls) -> str: return "my_name" def name_length_from_method(self) -> int: ...
Author   olirice
🌐
Tudelft
forum.kavli.tudelft.nl › programming questions
Abstract properties in Python's abstract base classes: good practices - Programming questions - Kavli institute discussions
July 30, 2020 - Question about software architecture. Below there is a snip from kwant’s system.py and its InfiniteSystem class, but it is not specific. I think that implicit definition of abstract properties in mixin/abstract classes is a bad coding practice, confusing for reading the code and when trying ...
🌐
Python.org
discuss.python.org › python help
Why does using property and abstractmethod not enforce properties in child? - Python Help - Discussions on Python.org
October 13, 2021 - Example: from abc import ABC, abstractmethod class AbstractExample(ABC): @property @abstractmethod def some_prop(self): pass class ConcreteExample(AbstractExample): def some_prop(self): …
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-base-class-abc-in-python
Abstract Base Class (abc) in Python - GeeksforGeeks
July 15, 2025 - We can use @property decorator and @abc.abstractmethod to declare properties as an abstract class.
🌐
GitHub
github.com › MichaelSchneeberger › dataclass-abc
GitHub - MichaelSchneeberger/dataclass-abc: A Python library that allows you to define abstract properties for dataclasses, bridging the gap between abstract base classes (ABCs) and dataclasses.
Here's how you can define an abstract ... Define an abstract base class with an abstract property class A(ABC): @property @abstractmethod def name(self) -> str: ......
Starred by 17 users
Forked by 4 users
Languages   Python 100.0% | Python 100.0%