How to create abstract properties in python abstract classes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python supports abstract properties through the abc module. The basic pattern is to combine @property with @abstractmethod, which tells subclasses that a property must be implemented before the class can be instantiated.
The Basic Abstract Property Pattern
A read-only abstract property looks like this:
The important part is the decorator order:
- '
@property' - '
@abstractmethod'
That defines a property whose implementation is required in concrete subclasses.
What Happens If You Do Not Implement It
If a subclass fails to implement the abstract property, Python keeps the subclass abstract and prevents instantiation.
That is the whole point of the abstract base class: it enforces an interface contract.
Abstract Setter Example
Sometimes the contract includes not just reading a property, but setting it as well. In that case, define both the getter and the setter as abstract where appropriate.
This pattern is useful when the API contract requires property-style access but still needs validation or state control.
Why Use an Abstract Property Instead of an Abstract Method
Use an abstract property when the subclass contract is about an attribute-like value rather than an action.
Good candidates include:
- dimensions
- configuration values
- identifiers
- computed attributes such as
area,length, oris_ready
If the abstraction feels like “do something,” an abstract method is usually clearer. If it feels like “expose a value,” an abstract property is often the better interface.
Abstract Properties Can Be Computed
The property does not have to map to a stored attribute. It can be computed dynamically.
This is one reason abstract properties fit well with interface design. They say what value must be exposed, not how the subclass must store it.
Prefer Clear Contracts Over Cleverness
Abstract properties are useful, but not every attribute should become one. If the class contract does not actually need property semantics, a plain method or a documented attribute may be simpler.
Use abstract properties when:
- subclasses must expose a consistent value-like API
- property access is the natural interface
- you want instantiation blocked until the contract is fulfilled
That keeps the abstraction intentional rather than decorative.
Common Pitfalls
The biggest mistake is getting the decorator order wrong. Put @property above @abstractmethod for the getter.
Another mistake is implementing the getter but forgetting the setter when the abstract contract includes one. In that case, the subclass remains abstract.
Developers also sometimes use abstract properties for values that would be clearer as ordinary methods. Not every required interface member needs property syntax.
Finally, remember that abstract base classes enforce structure at instantiation time, not at static type-check time only. A missing implementation causes a runtime TypeError when you try to create the subclass.
Summary
- Use
@propertytogether with@abstractmethodto define an abstract property. - Subclasses must implement the property before they can be instantiated.
- Add an abstract setter if the contract requires write access too.
- Abstract properties are best for value-like interfaces, not action-like behavior.
- Keep the interface clear: use property syntax when the abstraction is naturally attribute-shaped.

