Should super always be at the top of an __init__ method, or can it be at the bottom?
Solution 1:
This totally depends on the use case. Consider this.
class Foo():
def __init__(self):
print(self.name)
@property
def name(self):
return self.__class__.__name__
class Bar(Foo):
def __init__(self, name):
self.name = name
super().__init__()
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
self.__name = name
If you'd invoke super()
before setting self.name
within Bar.__init__
you'd get an AttributeError
because the required name has not yet been set.
Solution 2:
Is it bad form to have it at the bottom of an init method?
You're asking the wrong question. Regardless of whether it's bad from or not, there are valid use cases for moving the superclass initialization to the bottom of a sub-class's constructor. Where to put the call to the superclass's constructor entirely depends on the implementation of the superclass's constructor.
For example, suppose you have a superclass. When constructing the superclass, you want to give an attribute a certain value depending on an attribute of the subclasses:
class Superclass:
def __init__(self):
if self.subclass_attr:
self.attr = 1
else:
self.attr = 2
As you can see from above, we expect the subclasses to have the attribute subclass_attr
. So what does this mean? We can't initialize Supperclass
until we've given the subclasses the subclass_attr
attribute.
Thus, we have to defer calling the superclass's constructor until we initialize subclass_attr
. In other words, the call to super
will have to be put at the bottom of a subclasses constructor:
class Subclass(Superclass):
def __init__(self):
self.subclass_attr = True
super(Superclass, self).__init__()
In the end, the choice of where to put super
should not be based upon some style, but on what's necessary.