Multiple Inheritance Order

 A      B
| |   |   |
C D   E   F
| |   |   |
    G     H
      |
      I


user@ubuntu:~/Documents/Python/oop_python$ cat tt.py
class A:
    def call_me(self):
        print("A")

class C(A):
    def call_me(self):
        super().call_me()
        print("C")

class D(A):
    def call_me(self):
        super().call_me()
        print("D")

class B:
    def call_me(self):
        print("B")

class E(B):
    def call_me(self):
        super().call_me()
        print("E")

class F(B):
    def call_me(self):
        super().call_me()
        print("F")

class G(C, D, E):
    def call_me(self):
        super().call_me()
        print("G")

class H(F):
    def call_me(self):
        super().call_me()
        print("H")

class I(G, H):
    def call_me(self):
        super().call_me()
        print("I")


user@ubuntu:~/Documents/Python/oop_python$ python3.2 -i tt.py
>>> i = I()
>>> i.call_me()
A
D
C
G
I

Question Why B, Eare Fnot printed?

//            updated based on comments from delnan

user@ubuntu:~/Documents/Python/oop_python$ cat tt.py
class BaseClass():
    def call_me(self):
        print("BaseClass")
    pass

class A(BaseClass):
    def call_me(self):
        super().call_me()
        print("A")

class C(A):
    def call_me(self):
        super().call_me()
        print("C")

class D(A):
    def call_me(self):
        super().call_me()
        print("D")

class B(BaseClass):
    def call_me(self):
        super().call_me()
        print("B")

class E(B):
    def call_me(self):
        super().call_me()
        print("E")

class F(B):
    def call_me(self):
        super().call_me()
        print("F")

class G(C, D, E):
    def call_me(self):
        super().call_me()
        print("G")

class H(F):
    def call_me(self):
        super().call_me()
        print("H")

class I(G, H):
    def call_me(self):
        super().call_me()
        print("I")

user@ubuntu:~/Documents/Python/oop_python$ python3.2 -i tt.py
>>> i = I()
>>> i.call_me()
BaseClass
B
F
H
E
A
D
C
G
I
+5
source share
2 answers

A common misunderstanding is that super () will call all methods of superclasses. I will not. He will call only one of them. Which one is automatically calculated by super () in accordance with some specific rules. Sometimes the one he calls is not a real superclass, but a sibling. But there is no guarantee that everyone will be called if all classes, in turn, do not use super ().

A B . A, "" , B, , B "" ( , , ).

super(), A B, call_me, super(). ( , ).

, , , super(). , , . , , , mixin-. super().

+6

.

python:

  • , ,

  • - .

, . bfs traverse, . liner ( bfs traverse, ). G C D A E H F B BaseClass.

-:

( [--])

-, . python 3.x super(), , super (currentClassName, self). , bfs. , i.call_me() :

I.call_me → G.call_me → C.call_me → D.call_me → A.call_me → E.call_me → H.call_me → F.call_me → B.call_me → BaseClass. call_me

, B, E, F, " ", , A.call_me B.call_me - . , python .

, . python

+1

All Articles