Back
class A: def __init__(self): print("world")class B(A): def __init__(self): print("hello")B() # output: hello
class A:
def __init__(self):
print("world")
class B(A):
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.
super(self)
Use super(), as it returns a parent-like object in the new-style classes:
class A(object): def __init__(self): print "world"class B(A): def __init__(self): print "hello" super(B, self).__init__()B()shareedit
class A(object):
print "world"
print "hello"
super(B, self).__init__()
B()
shareedit
31k questions
32.8k answers
501 comments
693 users