I was looking at how Python implements the property descriptor internally. According to the docs property()
is implemented in terms of the descriptor protocol, repr
So you can use properties with inheritance?
Just an attempt at answering by giving an example:
class Base(object):
def __init__(self):
self._value = 0
@property
def value(self):
return self._value
@value.setter
def value(self, val):
self._value = val
class Child(Base):
def __init__(self):
super().__init__()
self._double = 0
@Base.value.setter
def value(self, val):
Base.value.fset(self, val)
self._double = val * 2
If it was implemented the way you write it, then the Base.value.setter
would also set the double, which is not wanted. We want a brand new setter, not to modify the base one.
EDIT: as pointed out by @wim, in this particular case, not only it would modify the base setter, but we would also end up with a recursion error. Indeed the child setter would call the base one, which would be modified to call itself with Base.value.fset
in an endless recursion.