class x(object):
    def __init__(self):
        self.x=1

class y(x):
    def __init__(self):
        super(y, self).__init__()

what is it doing "super" function in here?

Recommended Answers

All 4 Replies

The super() function allows you to access methods in the parent x that are shadowed by methods of the same name in the child y.

In this case, y.__init__ wants to call x.__init__. But the problem is that y.__init__ clobbers __init__ in the namespace; that is, x.__init__ doesn't get inherited. SO, super(y, self) returns x, so that your code means this:

class x(object):
    def __init__(self):
        self.x=1

class y(x):
    def __init__(self):
        x.__init__(self)

Jeff

class x(object):
    def __init__(self):
        self.x=1

class z(object):
    def __init__(self):
        self.z=1

class y(x,z):
    def __init__(self):
        super(y, self).__init__()

And what is it doing "super" function in here?

class x(object):
    def __init__(self):
        self.x=1

class z(object):
    def __init__(self):
        self.z=1

class y(x,z):
    def __init__(self):
        super(y, self).__init__()

And what is it doing "super" function in here?

It calls x.__init__():

class x(object):
    def __init__(self):
        print ("x.__init__()")
        self.x=1

class z(object):
    def __init__(self):
        print ("z.__init__()")
        self.z=1

class y(x,z):
    def __init__(self):
        super(y, self).__init__()

y()

"""my output -->
x.__init__()
"""

Super is not very useful. Call x.__init__() directly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.