0

I've tried to read python 2.7 docs, but no chance to undestand unfortunately.

Why does that happening? How it is connected with MRO and super call inside of init?

First example:

class SuperFirst(object):
    def __init__(self):
        super(SuperFirst, self).__init__()
        print "We were in SuperFirst's __init__"

class SuperSecond(object):
    def __init__(self):
        super(SuperSecond, self).__init__()
        print "We were in SuperSecond's __init__"

class JustThird(SuperFirst, SuperSecond):
    def __init__(self):
        super(JustThird, self).__init__()
        print "We were in JustThird's __init__"

JustThird()

This outputs:

We were in SuperSecond's __init__
We were in SuperFirst's __init__
We were in JustThird's __init__

Now lets remove the super call inside of the init of the first parent:

class SuperFirst(object):
    def __init__(self):
        print "We were in SuperFirst's __init__"

class SuperSecond(object):
    def __init__(self):
        super(SuperSecond, self).__init__()
        print "We were in SuperSecond's __init__"

class JustThird(SuperFirst, SuperSecond):
    def __init__(self):
        super(JustThird, self).__init__()
        print "We were in JustThird's __init__"

JustThird()

Output:

We were in SuperFirst's __init__
We were in JustThird's __init__

Now keep it only in the first parent:

class SuperFirst(object):
    def __init__(self):
        super(SuperFirst, self).__init__()
        print "We were in SuperFirst's __init__"

class SuperSecond(object):
    def __init__(self):
        print "We were in SuperSecond's __init__"

class JustThird(SuperFirst, SuperSecond):
    def __init__(self):
        super(JustThird, self).__init__()
        print "We were in JustThird's __init__"

JustThird()

Output:

We were in SuperSecond's __init__
We were in SuperFirst's __init__
We were in JustThird's __init__

Now remove from both parents:

class SuperFirst(object):
    def __init__(self):
        print "We were in SuperFirst's __init__"

class SuperSecond(object):
    def __init__(self):
        print "We were in SuperSecond's __init__"

class JustThird(SuperFirst, SuperSecond):
    def __init__(self):
        super(JustThird, self).__init__()
        print "We were in JustThird's __init__"

JustThird()

Output:

We were in SuperFirst's __init__
We were in JustThird's __init__
Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46
  • 1
    A [really nice and short answer](https://stackoverflow.com/questions/20545791/regarding-python-mro-and-how-super-behaves#answer-20546472) – dhke Aug 06 '16 at 08:34
  • 2
    What exactly don't you understand here? If there isn't a `super` call, the next implementation in the MRO doesn't get called and the chain stops. – jonrsharpe Aug 06 '16 at 08:36
  • @dhke thank you, so that's about MRO chain, not just base class... – Alexander.Iljushkin Aug 06 '16 at 11:19
  • @Alexezio You've got multiple inheritance, I'd say "base class" is everything after the current type in the MRO list. – dhke Aug 06 '16 at 11:36

0 Answers0