5

Notes:

  1. Getting the right inheritance order while printing the MRO of class D but not get getting the constructor call of class C.

Question: Why not printing C Constructor after A Constructor in the given code below?:

class A(object):

    def __init__(self):
        print("A Constructor")

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

class C():
    def __init__(self):
        print("C Constructor")
        super().__init__()

   def method(self):
        print("C method")  


class D(B, C):
    def __init__(self):
        print("D Constructor")
        super(D, self).__init__()
        super().method()
d = D()
print(D.__mro__)

Output: enter image description here

Someone
  • 444
  • 3
  • 12
  • 1
    I can't explain it so i don't post an answer, but the way to solve it is to change: `class D(B,C):` to `class D(C,B):` – U13-Forward Aug 08 '18 at 03:49
  • @U9-Forward: Yeah, able to print all required value while changing from class D(B, C) to class D(C, B). But I am wondering why the above code is not giving the expected answer. – Someone Aug 08 '18 at 03:53
  • Yep, that's why i didn't answer – U13-Forward Aug 08 '18 at 03:57
  • 1
    Possible duplicate of [How does Python's super() work with multiple inheritance?](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) – blhsing Aug 08 '18 at 04:43
  • @blhsing Getting the right order while printing the D.__mro__ but get not getting the constructor call of class C – Someone Aug 08 '18 at 04:59
  • The MRO is about lookup order, but when it comes to execution, it's a first-match-wins. When you instantiate `D`, it's looking for an `__init__` method through the classes in MRO, finds it immediately on `D`, executes that and stops. The rest is just explicit `super()` calls. If MRO did what you seem to expect it to do, your output would continue after `C method` with `B Constructor, A Constructor, A Constructor, C Constructor`. – shmee Aug 08 '18 at 05:35

2 Answers2

0

1 The init of C never been called cos

2 super starts searching on D goes D->B->A-> and ends on -> object

3 In this case the search starts D->B->A->object and the first super is done.

The object-or-type determines the method resolution order to be searched. The search starts from the class right after the type.

If mro of object-or-type is D -> B -> C -> A -> object and the value of type is B, then super() searches C -> A -> object

4 then second super() gives you access to methods in a superclass from the subclass that inherits from it.

5 In this case gives you access the the C.method and prints oue "C method" after the constructors of the first super are printed.

super() alone returns a temporary object of the superclass that then allows you to call that superclass’s methods allows you to swap out superclasses with minimal code changes.

Marios Keri
  • 98
  • 1
  • 8
0

if you want to know how MRO works in Python, I suggest you see this link.

In Python 2.3 and later, Python uses the C3 Linearization algorithm for the MRO.

Good luck.