12

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?

1
  • I think it is only example how to send arguments from one class to other using **kwds and remove shapename from **kwds - and Shape parent is not important in this example. Maybe Shape should have parent but it is not main element in all example.
    – furas
    Dec 5, 2017 at 18:57

2 Answers 2

10

The point is cooperative multiple inheritance. The point of the whole entire article is cooperative multiple inheritance, really.

You look at Shape and you don't see any parents besides object. Sure, but that doesn't mean there aren't any siblings, or anything else on the MRO after Shape. super() isn't just for superclasses; it searches for the next implementation of the method in the method resolution order. For example, one of the later classes in the article is

class MovableColoredShape(ColoredShape, MoveableAdapter):
    pass

In this case, Shape.__init__ needs to call super().__init__, or MoveableAdapter.__init__ and all further __init__ calls will be skipped.

8

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.

2
  • Thanks, reading up on diamond rule. Is it easy to explain why super().__init__() in shape knows to reference PositionMixin (sibling) rather than object's (parent) __init__()? Or should I just take this as is and acknowledge I don't know MRO very well at all? Dec 5, 2017 at 20:32
  • I don't have a good answer for you. I mean, the short answer is that Shape doesn't "know" anything; the call to super() returns a proxy object pointing at the appropriate class higher up in the MRO, and Shape is just operating on that. But that's a little handy wavy, and I don't know the gritty details of how Python objects are constructed to provide a better answer.
    – larsks
    Dec 5, 2017 at 20:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.