How to invoke the super constructor in Python?

337,928

Solution 1

In line with the other answers, there are multiple ways to call super class methods (including the constructor), however in Python-3.x the process has been simplified:

Python-3.x

class A(object):
 def __init__(self):
   print("world")

class B(A):
 def __init__(self):
   print("hello")
   super().__init__()

Python-2.x

In python 2.x, you have to call the slightly more verbose version super(<containing classname>, self), which is equivalent to super()as per the docs.

class A(object):
 def __init__(self):
   print "world"

class B(A):
 def __init__(self):
   print "hello"
   super(B, self).__init__()

Solution 2

super() returns a parent-like object in new-style classes:

class A(object):
    def __init__(self):
        print("world")

class B(A):
    def __init__(self):
        print("hello")
        super(B, self).__init__()

B()

Solution 3

With Python 2.x old-style classes it would be this:

class A: 
 def __init__(self): 
   print "world" 

class B(A): 
 def __init__(self): 
   print "hello" 
   A.__init__(self)

Solution 4

One way is to call A's constructor and pass self as an argument, like so:

class B(A):
    def __init__(self):
        A.__init__(self)
        print "hello"

The advantage of this style is that it's very clear. It call A's initialiser. The downside is that it doesn't handle diamond-shaped inheritance very well, since you may end up calling the shared base class's initialiser twice.

Another way is to use super(), as others have shown. For single-inheritance, it does basically the same thing as letting you call the parent's initialiser.

However, super() is quite a bit more complicated under-the-hood and can sometimes be counter-intuitive in multiple inheritance situations. On the plus side, super() can be used to handle diamond-shaped inheritance. If you want to know the nitty-gritty of what super() does, the best explanation I've found for how super() works is here (though I'm not necessarily endorsing that article's opinions).

Solution 5

Short Answer

super(DerivedClass, self).__init__()

Long Answer

What does super() do?

It takes specified class name, finds its base classes (Python allows multiple inheritance) and looks for the method (__init__ in this case) in each of them from left to right. As soon as it finds method available, it will call it and end the search.

How do I call init of all base classes?

Above works if you have only one base class. But Python does allow multiple inheritance and you might want to make sure all base classes are initialized properly. To do that, you should have each base class call init:

class Base1:
  def __init__():
    super(Base1, self).__init__()

class Base2:
  def __init__():
    super(Base2, self).__init__()

class Derived(Base1, Base2):
  def __init__():
    super(Derived, self).__init__()

What if I forget to call init for super?

The constructor (__new__) gets invoked in a chain (like in C++ and Java). Once the instance is created, only that instance's initialiser (__init__) is called, without any implicit chain to its superclass.

Share:
337,928
Mike
Author by

Mike

I hate computers

Updated on July 08, 2022

Comments

  • Mike
    Mike almost 2 years
    class A:
        def __init__(self):
            print("world")
    
    class B(A):
        def __init__(self):
           print("hello")
    
    B()  # output: hello
    

    In all other languages I've worked with the super constructor is invoked implicitly. How does one invoke it in Python? I would expect super(self) but this doesn't work.

  • Mike
    Mike about 14 years
    just of curiosity why does super(B,self) require both B and self to be mentioned? isn't this redundant? shouldn't self contain a reference to B already?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
    No, because self might actually be an instance of C, a child of B.
  • Iulius Curt
    Iulius Curt about 11 years
    What if B(A, AA)? I see that super(B,self) returns A, but then how do you get AA?
  • Daniel Martin
    Daniel Martin almost 11 years
    @iuliux: In that case, if self is an instance of B, then super(B, self).__init__() calls the method defined in A and super(A, self).__init__() calls the method defined in AA. If you're writing classes that might be multiply inherited from, you should always include a call to the super constructor, but which code that actually invokes depends on the inheritance decisions of your subclasses. See fuhm.net/super-harmful for more details.
  • Gabe
    Gabe almost 9 years
    @kdbanman: This will work with new-style classes, but one of the reasons to use new-style classes is to not have to do it this way. You can use super and not have to directly name the class you're inheriting from.
  • Luc
    Luc almost 9 years
    Addition to the post: "new-style classes" means python3.
  • Dan Getz
    Dan Getz almost 9 years
    @Luc not quite. It's a term used in Python 2. They were introduced in Python 2.2. You might be referring to the fact that they're the only available type of class in Python 3?
  • Luc
    Luc almost 9 years
    @DanGetz I don't really know. All I observed was that my code (using super(B,self).__init__(args)) didn't work using python 2.7.6, but it does work using python 3.4.0.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams almost 9 years
    @Luc: That's because the class was declared incorrectly. See my answer.
  • Luc
    Luc almost 9 years
    @IgnacioVazquez-Abrams Ah I see, there was no 'object' argument in A's constructor. My bad!
  • JojOatXGME
    JojOatXGME about 8 years
    With respect to the documentation of super(), you should be able to write super().__init__() wothout arguments.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 8 years
    @JojOatXGME: In 3.x, yes. 2.x still needs the arguments.
  • JojOatXGME
    JojOatXGME about 8 years
    @IgnacioVazquez-Abrams Ok. Maybe you should mention that you are using python 2.x in your answer? I'm relative new to python and I think it is a little bit confusing that many sites do not write whether they are using python 2 or 3.?
  • Andrew Hows
    Andrew Hows over 7 years
    The original post was written six years ago; python 3 was barely a thing at that point.
  • router
    router over 7 years
    What is the rationale behind passing B (child Class) and self (child like object) to the super method? Ex.: super(B, self).__init__().
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 7 years
    @router: Python 2.x didn't use introspection in super to figure out what the relevant class and object were, so you had to pass them.
  • borgr
    borgr over 6 years
    Why is the super function called last (I switching the order and python just ignored the code after the super_init, why is that?)
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 6 years
    @borgr: Because we want "hello" to print before "world". If this isn't an issue in your code then call it whenever appropriate.
  • chepner
    chepner over 4 years
    @Gabe That's one of the least important reasons to use super.
  • Danijel
    Danijel over 4 years
    Finaly: what will the above code output? That should be added to the answer.
  • Charlie Parker
    Charlie Parker over 4 years
    your answer is the most viewed. Wouldn't it be useful to extend your answer to explain what is the best practice for python 3? I believe it's: don't use derived class name do this in python 3 (pseudocode): super().__init__(args...)