0 votes
1 view
in Python by (8.4k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...