Sub topic 14
Overriding properties in subclasses
This chapter assumes you can write @property getters and setters, and that you know basic inheritance and super().
A property is a class attribute
A property lives on the class, not on the instance. When Python reads obj.balance, it searches the class hierarchy in method resolution order and uses the first balance it finds. If a subclass defines balance, that definition hides the parent's property entirely.
The base class
class Account:
def __init__(self, balance):
self.balance = balance
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
if value < 0:
raise ValueError("balance cannot be negative")
self._balance = valuePitfall: redefining only the getter
A new @property in the subclass creates a brand new property with no setter. The parent's setter is gone.
class ReadOnlyAccount(Account):
@property
def balance(self):
return self._balance
try:
ReadOnlyAccount(10)
except AttributeError:
print("setter was lost")The failure happens inside Account.__init__, because self.balance = balance now finds a property without a setter.
Overriding one part of a property
To change only the setter and keep the inherited getter, use the parent's property as the decorator base. Account.balance.setter returns a copy of the property with the new setter, and the copy is assigned to the subclass. The parent's property is not modified.
class Capped(Account):
@Account.balance.setter
def balance(self, value):
if value > 1000:
raise ValueError("balance above cap")
Account.balance.fset(self, value)
c = Capped(500)
print(c.balance) # 500
for bad in (2000, -5):
try:
c.balance = bad
except ValueError as exc:
print(exc) # balance above cap, then balance cannot be negativeReading Account.balance on the class returns the property object itself. Its fget, fset, and fdel attributes hold the original functions, so the new setter can call the parent's validation through fset.
The same copy-and-replace form exists for the other parts: @Account.balance.getter and @Account.balance.deleter.
Parent code sees the override
Methods in the base class read self.balance, so they use whichever property the instance's class provides. If Account.__init__ assigns self.balance, a subclass setter runs during construction. That is why Capped(2000) fails, as it should. It also means a subclass setter must not depend on state that __init__ has not created yet.
Summary of the choices
| Goal | Approach |
|---|---|
| Replace the whole property | Define a new `@property` in the subclass |
| Change only the setter | `@Parent.name.setter` |
| Change only the getter | `@Parent.name.getter` |
| Reuse the parent's behavior | `Parent.name.fget(self)` or `.fset(self, value)` |
Exercise
A subclass defines @property def balance(self): return self._balance * 2 and nothing else. The parent has a balance setter. What happens on sub.balance = 5, and how do you fix it while keeping the doubled getter?
Show answer
It raises AttributeError, because the subclass property has no setter and hides the parent's property. Keep both by writing @Account.balance.getter above the new getter, which copies the parent's property with the new getter and keeps the inherited setter. Alternatively, define a setter in the subclass with @balance.setter.