How to invoke the super constructor in Python?

Mike picture Mike · Mar 8, 2010 · Viewed 287.1k times · Source
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.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Mar 8, 2010

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()