Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

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.

1 Answer

0 votes
by (16.8k points)

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

Related questions

0 votes
1 answer
asked Oct 17, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...