Sunday, 29 April 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...