首页 > 代码库 > TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases A2, A1 出现原因及其解决办法

TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases A2, A1 出现原因及其解决办法

原本想测试继承,出现了这个错误:

  源代码

 1 class A1(object):
 2     def fo1(self):
 3         print "i‘m A1"
 4 class A2(object):
 5     def fo1(self):
 6         print "i‘m A2"
 7 class B1(A1,A2):
 8     def bar(self):
 9         print "i‘m B1"
10 class B2(A2,A1):
11     def bar(self):
12         print "i‘m B2"
13 class C(B1,B2):#提示出现错误的地方
14     pass
15 if __name__==__main__:
16     m=C()
17     print C.__mro__
18     m.bar()
19     m.fo1()

错误行数出现在13行,实际就是构图的时候产生了错误,(根据继承先后广搜构图)

此时构图为技术分享

显然不能出现俩A1,因此会出现如上的报错

要B都与A有联系只能是下面这种继承构图才不会出错技术分享

即将代码修改为

class A1(object):
    def fo1(self):
        print "i‘m A1"
class A2(object):
    def fo1(self):
        print "i‘m A2"
class B1(A1,A2):
    def bar(self):
        print "i‘m B1"
class B2(A1,A2):
    def bar(self):
        print "i‘m B2"
class C(B1,B2):
    pass
if __name__==__main__:
    m=C()
    print C.__mro__
    m.bar()
    m.fo1()

 

TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases A2, A1 出现原因及其解决办法