C3算法

面向對象的多繼承python

先找左 ,再找右算法

class A(object):
    pass


class B(object):
    def f1(self):
        print('B')


class C(A,B):
    pass

obj = C()

obj.f1()

 

經典類對象

新式類blog

經典類和新式類的查找成員的順序不同。繼承

經典類,一條道走到黑(深度優先)。class

新式類,C3算法實現object

class A(object):
    pass

class B(A):
    pass

class C(B):
    pass

class D(object):
    pass

class E(D,C):
    pass

class F(object):
    pass

class G(F):
    pass

class H(C,G):
    pass

class Foo(E,H):
    pass


# print(E.__mro__)
# print(H.__mro__)
"""
L(Foo + L(E)  + L(H) )



L(E) = E,D,C,B,A,object
L(H) = H,C,B,A,G,F,object

Foo = (object) + (G,F,object)
Foo,E,D,H,C,B,A,G,F,object
"""
print(Foo.__mro__)
相關文章
相關標籤/搜索