Given a Parent
class with value
property, Child
can inherit and overload the property while accessing Parent
property getter and setter.
Although we could just reimplement the Child.value
property logic completely without using Parent.value
whatsover, this would violate the DRY principle and, more important, it wouldn't allow for proper multiple inheritance (as show in the example property_inheritance.py
bellow).
Two options:
-
Child
redefinesvalue
property completely, both getter and setter. -
Child
usesParent.value
property, and only overloads setter.Parent
class must be referenced explicitly.
In either pattern, using Parent.value.getter
is as simple as
return super().value
However, accessing Parent.property.setter
is more verbose:
super(Child, type(self)).value.fset(self, new_value)
It would be nice to just super().value = new_value
but this won't work due to super
current implementation (as Python 3.7).
There is a Python Issue regarding this rather unintuitive behaviour.
Thank you so much! Usually, during tutorials only super() implemented on int and str are shown which doesn't explain syntax for other methods. This really helped me!