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
class C(ABC): @property @abstractmethod def my_abstract_property(self): ... 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:
People also ask

Can an abstract class have properties in Python?
Yes, an abstract class in Python example can have abstract properties, which must be implemented by the subclass, just like abstract methods.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
How can we implement an abstract class in Python with a constructor?
You can define a constructor in an abstract class in Python example like any other class. However, the constructor cannot be used directly until the class is subclassed and instantiated.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
What is an abstract class in Python?
An abstract class in Python is a class that cannot be instantiated directly. It can include abstract methods that any subclass must implement.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
🌐
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 especially helpful. ... from abc import ABC, abstractmethod class Animal(ABC): @property @abstractmethod def sound(self): pass class Bird(Animal): @property def sound(self): return "Chirp" bird = Bird() print(bird.sound) # Chirp
🌐
The Teclado Blog
blog.teclado.com › python-abc-abstract-base-classes
How to Write Cleaner Python Code Using Abstract Classes
October 26, 2022 - 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 ...
🌐
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 = ...
🌐
Python
docs.python.org › 3.8 › library › abc.html
abc — Abstract Base Classes — Python 3.8.20 documentation
class Descriptor: ... @property def __isabstractmethod__(self): return any(getattr(f, '__isabstractmethod__', False) for f in (self._fget, self._fset, self._fdel)) ... Unlike Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the ...
Find elsewhere
🌐
Python Module of the Week
pymotw.com › 2 › abc
abc – Abstract Base Classes - Python Module of the Week
import abc class Base(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def value(self): return 'Should never get here' class Implementation(Base): @property def value(self): return 'concrete property' try: b = Base() print 'Base.value:', b.value except Exception, err: print 'ERROR:', str(err) i = Implementation() print 'Implementation.value:', i.value · The Base class in the example cannot be instantiated because it has only an abstract version of the property getter method. $ python abc_abstractproperty.py ERROR: Can't instantiate abstract class Base with abstract methods value Implementation.value: concrete property
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
September 12, 2024 - ... class Animal(ABC): @property @abstractmethod def sound(self): pass # Create a subclass of Animal class Dog(Animal): def __init__(self, name): self._name = name # Implement the abstract property @property def sound(self): retu
🌐
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()
🌐
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 ...
🌐
Geek Python
geekpython.in › abc-in-python
Python's ABC: Understanding the Basics of Abstract Base Classes
October 29, 2023 - Python doesn’t allow creating objects for abstract classes because there is no actual implementation to invoke rather they require subclasses for implementation. ... We got the error stating that we cannot instantiate the abstract class Details with abstract methods called getname and getrole. 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.
🌐
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.
Use the @property decorator alongside a setter to define mutable properties in the abstract class. from abc import ABC, abstractmethod from dataclassabc import dataclassabc class A(ABC): @property @abstractmethod def name(self) -> str: ...
Starred by 17 users
Forked by 4 users
Languages   Python 100.0% | Python 100.0%
🌐
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
🌐
datagy
datagy.io › home › python posts › python abc: abstract base class and abstractmethod
Python abc: Abstract Base Class and abstractmethod • datagy
December 23, 2022 - This process is a bit more involved, so let’s take a look at an example. # Adding Properties to Abstract Base Classes from abc import ABC, abstractmethod class Employee(ABC): @abstractmethod def __init__(self, name): self._name = name @property @abstractmethod def name(self): pass @name.setter @abstractmethod def name(self, value): pass class Manager(Employee): def __init__(self, name): self._name = name @property def name(self): return self._name @name.setter def name(self, value): self._name = value nik = Manager('Nik') print(nik.name) # Returns: Nik
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.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] # 100 upvotes) How to create abstract properties in python a...