python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python 继承冲突及继承顺序

一文讲解python中的继承冲突及继承顺序

作者:宇航员写代码

python支持多继承,如果子类没有重写方法,则默认会调用父类的方法,本文主要介绍了一文讲解python中的继承冲突及继承顺序,具有一定的参考价值,感兴趣的可以了解一下

简单的菱形继承

设计类如下

设计代码:

class Animal(object):
    def __init__(self, age: int = None, gender: int = None) -> None:
        print("Call the constructor of Animal.")
        self.m_age = age
        self.m_gender = gender
        self.m_name = "Animal"
        print("Ends the call to Animal's constructer.")
        pass

    def eat(self):
        print("Animal is eating")

    def sleep(self):
        print("Animal is sleeping")


class Tiger(Animal):
    def __init__(self, age: int = None, gender: int = None) -> None:
        print("Call the constructor of Tiger.")
        self.m_name = "Tiger"
        super().__init__(age, gender)
        print("Ends the call to Tiger's constructer.")

    def eat(self):
        print("Tiger is eating")

    pass


class Lion(Animal):
    def __init__(self, age: int = None, gender: int = None) -> None:
        print("Call the constructor of Lion.")
        self.m_name = "Lion"
        super().__init__(age, gender)
        print("Ends the call to Lion's constructer.")

    def eat(self):
        print("Lion is eating")

    def sleep(self):
        print("Lion is sleeping")
    pass


class Liger(Tiger, Lion):
    def __init__(self, age: int = None, gender: int = None) -> None:
        super().__init__(age, gender)
    pass


if __name__ == '__main__':
    liger = Liger(8, 1) #实例化一个`Liger`
    print(Liger.__mro__)
    print(liger.m_name)
    liger.eat()
    liger.sleep()

运行输出为:

Call the constructor of Tiger.
Call the constructor of Lion.
Call the constructor of Animal.
Ends the call to Animal's constructer.
Ends the call to Lion's constructer.
Ends the call to Tiger's constructer.
(<class '__main__.Liger'>, <class '__main__.Tiger'>, <class '__main__.Lion'>, <class '__main__.Animal'>, <class 'object'>)
Animal
Tiger is eating
Lion is sleeping

小结:对于简单的菱形继承,可以大致认为其是在类型树上按照"从左到右,从上到下"的广度优先遍历顺序查找成员的。

复杂的菱形继承

对于复杂的菱形继承,有时候按照上面的广度优先遍历类型树得到的继承顺序并不正确。例如:

这时如果使用广度优先遍历得到的继承顺序为:M A B Z X Y object

运行如下Python代码得到的继承顺序为:

class X(object):
    pass

class Y(object):
    pass


class Z(object):
    pass


class A(X, Y):
    pass


class B(Y, Z):
    pass


class M(A, B, Z):
    pass

print(M.mro())

# [<class '__main__.M'>, 
<class '__main__.A'>,
<class '__main__.X'>, 
<class '__main__.B'>,
<class '__main__.Y'>, 
<class '__main__.Z'>,
<class 'object'>]

继承顺序为M A X B Y Z object

查阅资料得知,这是因为在Python2.3以后的版本,类的继承顺序求法采用了C3算法,以保证继承的单调性原则。(子类不能改变基类的MRO搜索顺序)

MRO C3 算法

算法原理

C3(C3 linearization)算法实现保证了三种重要特性:

在C3算法中,把L[C]定义为类C的的linearization值(也就是MRO里的继承顺序,后面简称L值),计算逻辑如下:

L[C] = C + merge of linearization of parents of C and list of parents of C in the order they are inherited from left to right. 

L[C]是所有父类的L值的merge

运算规则为:

其中 C 多继承父类 B 1 . . B N

merge的运算方法如下:

重复上述步骤直至列表为空或者不能找出可以输出的元素。

算法例子

拿上面的继承来举例:

从上至下一次计算object,X,Y,Z,A,B,M的继承顺序:

L[object] = O(object)

L[X] = X + merge(L[object]) = X + O = XO,同理L[Y] = YOL[Z] = ZO

L[A] = A+merge(L[X],L[Y],XY)
     = A + merge(XO,YO,XY) 
	 = AX + merge(O,YO,Y)
	 = AXY + merge(O,O)
	 = AXYO

L[B] = B + merge(L[Y],L[Z],YZ)
     = B + merge(YO,ZO,YZ)
     = BY + merge(O,ZO,Z)
     = BYZ + merge(O,O)
     = BYZO

然后是M的继承顺序计算:

L[M] = M + merge(L[A],L[B],L[Z],ABZ)
     = M + merge(AXYO,BYZO,ZO,ABZ)
     = MA + merge(XYO,BYZO,ZO,BZ)
     = MAX + merge(YO,BYZO,ZO,BZ) #第一个参数中Y被第二个参数中的Z继承,所以检查第二个参数的第一个元素即 B
     = MAXB + merge(YO,YZO,ZO,Z)
     = MAXBY + merge(O,ZO,ZO,Z) #同样,O被Z继承
     = MAXBYZ + merge(O,O,O)
     = MAXBYZO

得到类M的最终继承顺序MAXBYZO

算法实现

下面是其它资料中找到的Wiki百科上对该算法的Python版本实现:

def c3MRO(cls):
    if cls is object:
        # 讨论假设顶层基类为object,递归终止
        return [object]

    # 构造C3-MRO算法的总式,递归开始
    mergeList = [c3MRO(baseCls) for baseCls in cls.__bases__]
    mergeList.append(list(cls.__bases__))
    mro = [cls] + merge(mergeList)
    return mro


def merge(inLists):
    if not inLists:
        # 若合并的内容为空,返回空list
        # 配合下文的排除空list操作,递归终止
        return []

    # 遍历要合并的mro
    for mroList in inLists:
        # 取head
        head = mroList[0]
        # 遍历要合并的mro(与外一层相同),检查尾中是否有head
        ### 此处也遍历了被取head的mro,严格地来说不符合标准算法实现
        ### 但按照多继承中地基础规则(一个类只能被继承一次),
        ### head不可能在自己地尾中,无影响,若标准实现,反而增加开销
        for cmpList in inLists[inLists.index(mroList) + 1:]:
            if head in cmpList[1:]:
                break
        else:
            # 筛选出好head
            nextList = []
            for mergeItem in inLists:
                if head in mergeItem:
                    mergeItem.remove(head)
                if mergeItem:
                    # 排除空list
                    nextList.append(mergeItem)
            # 递归开始
            return [head] + merge(nextList)
    else:
        # 无好head,引发类型错误
        raise TypeError

测试:

class A(object):pass
class B(object):pass
class C(object):pass
class E(A,B):pass
class F(B,C):pass
class G(E,F):pass

print([i.__name__ for i in c3MRO(G)])

输出结果:

['G', 'E', 'A', 'F', 'B', 'C', 'object']

参考资料

到此这篇关于一文讲解python中的继承冲突及继承顺序的文章就介绍到这了,更多相关python 继承冲突及继承顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文