1

in How does Python's super() work with multiple inheritance? one answer explains the reason why, for this example:

class First(object):
  def __init__(self):
    super(First, self).__init__()
    print "first"

class Second(object):
  def __init__(self):
    super(Second, self).__init__()
    print "second"

class Third(First, Second):
  def __init__(self):
    super(Third, self).__init__()
    print "that's it"

the answer is:

>>> x = Third()
second
first
that's it

According to the explanation, it is because:

Inside __init__ of First super(First, self).__init__() calls the __init__ of Second, because that is what the MRO dictates!

What does he mean? Why does calling First super __init__ will call __init__ of Second? I think First has nothing to do with Second?

It is said that: "because that is what the MRO dictates", I read https://www.python.org/download/releases/2.3/mro/ but still have no clue.

Can anybody explain?

2
  • 2
    Contrary to the name, super does not refer to the superclass. It doesn't matter whether First or Second have anything to do with each other. Commented Sep 20, 2017 at 20:05
  • ok. I see. super means following the MRO order. Commented Sep 20, 2017 at 20:25

1 Answer 1

3

It doesn't matter what the MRO (method resolution order) of the individual classes is. The only thing that matters (if you use super) is the MRO of the class you call the method on.

So when you call Third.__init__ it will follow Third.mro which is Third, First, Second, object:

>>> Third.mro()
[__main__.Third, __main__.First, __main__.Second, object]

So any super will look up the next superclass in the MRO, not the superclass of the actual class you're "in".

Sign up to request clarification or add additional context in comments.

1 Comment

Now I understand. It is so confusing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.