I'm wondering if there is any use for explicitly using the getter decorator for class properties:
class A:
@property
def p(self):
return self._p
@p.setter
def p(self, val):
assert p < 1000
self._p = val
@p.getter
def p(self):
return self._p
why would I ever explicitly use the getter here? Doesn't it render the code within p(self) unreachable? Basically, is there any practical reason to use it?
It's useful when you inherit a property from a parent class and want to override the getter:
class Parent:
@property
def prop(self):
return 'Parent'
@prop.setter
def prop(self, value):
print('prop is now', value)
class Child(Parent):
@Parent.prop.getter
def prop(self):
return 'Child'
This creates a copy of Foo.prop
with a different getter function but the same setter (and deleter).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With