Sunday, April 29, 2018

class - How to invoke the super constructor in Python?



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



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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...