There are two common approaches to calling the parent __init__()
from a child:
- With an explicit call to the Parent:
class Child(Parent):
def __init__(self):
Parent.__init__(self)
- With an implicit call to the Parent, using
super()
:
class Child(Parent):
def __init__(self):
super().__init__(self)
The second approach has an advantage: if instead of inheriting from Parent, you wanted to inherit from Parent2, you'd simply have to change one line. In the first approach, you'd have to to change two.
My question is as follows: can the same reasoning be applied to decorators?
Consider the following base class:
class Parent:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
@value.setter:
def value(self, value_new):
self._value = value_new
If I wanted to override only the getter in a child class, I'd do the following:
class Child(Parent):
@Parent.value.getter # Explicit call to the parent class
def value(self):
return self._value
Is there a way to avoid explicitly calling the parent? (Just like we did at the top)
The code below doesn't work, but is there something similar that would?
class Child(Parent):
@super.value.getter # super instead of explicit call to Parent
def value(self):
return self._value