Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should super always be at the top of an __init__ method, or can it be at the bottom?

Tags:

python

In most Python examples, when super is used to call a parent class's constructors, it appears at the top.

Is it bad form to have it at the bottom of an init method?

In the examples below, super is at the bottom of A's constructor, but at the top of B's constructor.

class A:
    def __init__(self):
        # Do some stuff
        b = result_of_complex_operation()
        super(A, self).__init__(b)

class B:
    def __init__(self):
        super(A, self).__init__(b)
        # Do some stuff
like image 452
Ginger Avatar asked Aug 16 '17 20:08

Ginger


2 Answers

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.

like image 105
Richard Neumann Avatar answered Oct 13 '22 23:10

Richard Neumann


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.

like image 37
Christian Dean Avatar answered Oct 14 '22 01:10

Christian Dean