0

I recently had a problem understanding the code below:

class A(object):
    def __init__(self):
        print("go A go!")

class B(A):
    def __init__(self):
        super(B, self).__init__()
        print("go B go!")

class C(A):
    def __init__(self):
        super(C, self).__init__()
        print("go C go!")


class D(B,C):
    def __init__(self):
        super(D, self).__init__()
        print("go D go!")
d = D()

The result I got is:

go A go!
go C go!
go B go!
go D go!

I understand the use of super() in simple multiple-inheritance but I really don't know how it got this result.

1 Answer 1

3

Python uses C3 linearlization to define the order (Method Resolution Order ). It produced the following order:

D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, builtins.object)

Since you are calling the super first and the printing, it results the printing in reverse order (similar to call order in recursion)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.