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
🌐
GitHub
github.com › python › mypy › issues › 4426
Should we always prohibit "abstract" attributes? · Issue #4426 · python/mypy
November 20, 2017 - Currently, instantiating explicit subclasses of protocols with "abstract" attributes is prohibited: class P(Protocol): x: int # Note, no r.h.s. class C(P): pass class D(P): def __init__(self) -> None: self.x = 42 C() # E: Cannot instanti...
Author   ilevkivskyi
🌐
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.
Discussions

Is there a such thing as declaring an attribute of an abstract class in Python?
You can do this with @property and @abstractmethod Py:3.3+ (@abstractproperty for earlier versions of python): from abc import ABC, abstractmethod class Base(ABC): @property @abstractmethod def name(self): pass class Concrete(Base): def __init__(self, name): self._name = name @property def name(self): return self._name In []: Base() Out[]: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In [56], line 1 ----> 1 Base() TypeError: Can't instantiate abstract class Base with abstract method name In []: Concrete("Name").name Out[]: 'Name' More on reddit.com
🌐 r/learnpython
24
9
February 9, 2023
Abstract properties in Python's abstract base classes: good practices
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 ... More on forum.kavli.tudelft.nl
🌐 forum.kavli.tudelft.nl
1
July 30, 2020
Abstract variables in abc
the ability to create abstract variables so you can encourage the user to override a class variable so you can encourage a user to override it More on discuss.python.org
🌐 discuss.python.org
0
October 24, 2024
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
People also ask

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
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 do abstract classes help in Python?
Abstract classes in Python help enforce a structure for subclasses, ensuring that all required methods are implemented making code more organized and maintainable.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
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.

🌐
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 ...
🌐
AlgoMaster
algomaster.io › learn › python › abstract-classes
Abstract Classes | Python | AlgoMaster.io | AlgoMaster.io
January 3, 2026 - Instantiation: Neither abstract classes nor interfaces can be instantiated directly, but abstract classes can have concrete methods, while interfaces cannot. Multiple Inheritance: Python supports multiple inheritance, so a class can implement multiple abstract classes.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Abstract variables in abc - Python Help - Discussions on Python.org
October 24, 2024 - the ability to create abstract variables so you can encourage the user to override a class variable so you can encourage a user to override it
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › abstract class in python
Abstract Class in Python | With Example and Interface Comparison
June 1, 2025 - The sound property is decorated with both @property and @abstractmethod, making it an abstract property. An abstract property is similar to an abstract method, but it is used to manage an attribute that will be defined in subclasses.
🌐
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…
🌐
Python Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no 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.
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
Data attributes may be referenced by methods as well as by ordinary users (“clients”) of an object. In other words, classes are not usable to implement pure abstract data types. In fact, nothing in Python makes it possible to enforce data hiding — it is all based upon convention.
🌐
Atlas
atlas.org › solution › 60159698-8fdd-4e38-b20e-f0e700a7560a › what-is-an-abstract-class-in-python
Solved: What is an abstract class in Python?
Atlas is the most accurate AI assistant for school. Score higher and stress less with the only assistant trained on your class materials. Free to access.
🌐
Real Python
realpython.com › ref › glossary › abstract-base-class
abstract base class (ABC) | Python Glossary – Real Python
This is useful for ensuring that derived classes implement particular methods from the base class, providing a consistent interface for different parts of your program. To define an abstract base class, you inherit from abc.ABC and use the @abstractmethod decorator to mark methods that must be implemented by subclasses.
🌐
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…
🌐
Python Tutorial
pythontutorial.net › home › python oop › python abstract classes
Python Abstract Class
March 31, 2025 - Use abc module to define abstract classes. Was this tutorial helpful ? Yes No · Previously · Python __slots__ Up Next · Python Protocol · Python Object-oriented Programming · Class · Class Variables · Instance Methods · __init__: Initializing Instance Attributes ·
🌐
Pybites
pybit.es › articles › elevate-your-python-harnessing-the-power-of-abstract-base-classes-abcs
Elevate Your Python: Harnessing The Power Of Abstract Base Classes (ABCs) - Pybites
February 21, 2024 - Therefore, when you attempt to instantiate IncompleteSearch, Python raises shown TypeError, indicating that IncompleteSearch can’t be instantiated because it’s still “abstract” due to the unimplemented match_content method. Key to this pattern is the @abstractmethod decorator. This decorator, applied to a method within an abstract base class, marks the method as one that must be overridden in any concrete subclass.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › python › python_abstract_base_classes.htm
Python - Abstract Base Classes
An Abstract Base Class (ABC) in Python is a class that cannot be instantiated directly and is intended to be subclassed. ABCs serve as blueprints for other classes by providing a common interface that all subclasses must implement.