On the problem of multiple inheritance of Python, why are the results so different?

related Code 1:

class A(object):
    def show(self):
        print ("init A...")

class B(A):
    def show(self):
        super(B, self).show()
        print("init B...")
class C(A):
    def show(self):
        -sharp super(C, self).show()
        print("init C...")

class D(B, C):
    def show(self):
        super(D, self).show()
        print("init D...")
d = D()
d.show()
The result of

output is

init C...
init B...
init D...

what I want to ask here is why I didn"t go through An and output init A.

.

related Code 2:

class A(object):
    def show(self):
        print ("init A...")

class B(A):
    def show(self):
        super(B, self).show()
        print("init B...")
class C(A):
    def show(self):
        -sharp super(C, self).show()
        print("init C...")

class D(C, B):    -sharp1
    def show(self):
        super(D, self).show()
        print("init D...")
d = D()
d.show()
The result of

output is

init C...
init D...

what I want to ask here is why the method in B has not been called?
there are also new MRO algorithms that use breadth-first search. How does it work here?

Nov.14,2021

python.jobbole.com/85685/" rel=" nofollow noreferrer "> http://python.jobbole.com/85685/ is quite clear.
the first case mro is dbca, and the second case is dcba. In the first, super, is called in b, so it goes to the show of c in the order of mro. In the second type, there is no call to super in c, so it ends.


in the first case, you can see the order in which the output is DBCA with D.

(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

the second is similar.
super should follow the order of mro.

Menu