Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling `super()` in parent class

I'm reading Raymond Hettinger's Python’s super() considered super! About a page in, there's this example:

class Shape:
    def __init__(self, shapename, **kwds):
        self.shapename = shapename
        super().__init__(**kwds)        

class ColoredShape(Shape):
    def __init__(self, color, **kwds):
        self.color = color
        super().__init__(**kwds)

cs = ColoredShape(color='red', shapename='circle')

Why is it necessary to call super() in Shape here? My understanding is that this calls object.__init__(**kwds) since Shape implicitly inherits from object.

Even without that statement, we've already

  • established shapename already in the parent's __init__,
  • established the child class's color in an explicit method override,
  • then invoked the parent's __init__ with super() in ColoredShape.

As far as I can tell, dropping this line produces the same behavior & functionality:

class Shape:  # (object)
    def __init__(self, shapename, **kwds):
        self.shapename = shapename
        # super().__init__(**kwds)

class ColoredShape(Shape):
    def __init__(self, color, **kwds):
        self.color = color
        super().__init__(**kwds)
    def check(self):
        print(self.color)
        print(self.shapename)

cs = ColoredShape(color='red', shapename='circle')

cs.check()
# red
# circle

What is the purpose of super() within Shape here?

like image 459
Brad Solomon Avatar asked Dec 05 '17 18:12

Brad Solomon


People also ask

How do you call super in parent class?

2) super can be used to invoke parent class method In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local. To call the parent class method, we need to use super keyword.

What is the role of super between a parent child class?

Advantages of super keyword: It can be used to access the data members of parent class when both parent and child have member with same name. It is used to prevent overriding the parent method. It can be used to call parameterized constructor of parent class.

How do you call super super class in Python?

Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by 'super'.

Is Super a parent class?

They are essentially the same. Depending on the language, the terminology changes. Parent may mean the immediate parent, while Super class may mean any of the ancestor classes.


1 Answers

I see that @user2357112 has already provided a correct answer. I was working on an example that I'd though I'd leave here because it's pretty much what user2357112 is describing. Consider a mixin class like this:

class PositionMixin:
    def __init__(self, x=0, y=0, **kwds):
        super().__init__(**kwds)
        self.x = x
        self.y = y

Let's say you apply that to your ColoredShape class:

class ColoredShape(Shape, PositionMixin):
    def __init__(self, color, **kwds):
        self.color = color
        super().__init__(**kwds)

If Shape doesn't call super.__init__, then when you do this:

myshape = ColoredShape('red', shapename='circle', x=1, y=1)
print(myshape.x, myshape.y)

You get:

Traceback (most recent call last):
  File "supertest.py", line 18, in <module>
    print (myshape.x, myshape.y)
AttributeError: 'ColoredShape' object has no attribute 'x'

The call to super.__init__ in shape is necessary to call the __init__ method on PositionMixin.

like image 147
larsks Avatar answered Oct 29 '22 20:10

larsks