Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for
来源:互联网 发布:淘宝主图背景图模板 编辑:程序博客网 时间:2024/06/09 18:16
今天在学习python的时候遇到了 Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases P2,P1这个异常,然后观察了一下代码发现了问题,原因是我写多重继承的时候因为父类的顺序问题导致了python的方法解析顺序出现了问题。
原代码如下:
class P1(object): def foo(self): print 'called P1-foo()'class P2(object): def foo(self): print 'called P2-foo()' def bar(self): print 'called P2-bar()'class C1(P2,P1): passclass C2(P1,P2): def bar(self): print 'called C2-bar()'class GC(C1,C2):#这个类如果去掉的话也可以没有错误,如果加上这个类后,按照这种写法,会导致mro中无法在唯一确认类之间的关系,会导致继承关系混乱(我的理解,由于类的祖先类的查找顺序是广度优先,而不是尝试优先,所以它无法确认P1,P2在祖先图谱中的顺序) pass
其中红色的地方就是出错的地方。把p1与p2换下位置就好了。
我认为原理如下:
就像上面GC类后面的注释一样(
这个类如果去掉的话也可以没有错误,如果加上这个类后,按照这种写法,会导致mro中无法在唯一确认类之间的关系,会导致继承关系混乱(我的理解,由于类的祖先类的查找顺序是广度优先,而不是尝试优先,所以它无法确认P1,P2在祖先图谱中的顺序)
),如果加上GC类后它们的图谱关系就如图
python是无法确认P1在图中的具体位置的。所以会报出MRO错误。
原文链接:http://www.cnblogs.com/yudy/archive/2013/06/10/3130521.html
注:以上现象是在经典类中出现的,在新式类中没有此问题。但是可能你不能调用到你想调用的方法。
补充:
这个问题在python 2.7的经典类中依然存在!
0 0
- Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for
- TypeError: Cannot create a consistent method resolution
- Python中class的MRO(Method Resolution Order)
- Python MRO(Method Resolution Order)方法解析顺序
- What’s the behavior when calling an overwritten virtual method in the constructor method of a base class?
- AWS CLI S3 A client error (403) occurred when calling the HeadObject operation: Forbidden
- error:The calling thread cannot access this object because a different thread owns it解决方法
- "Error calling method of a PBNI Object."的解决
- error:Cannot create file when that file already exists_
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visua
- 关于error:Cannot assign to 'self' outside of a method in the init family
- 关于error:Cannot assign to 'self' outside of a method in the init family
- 关于error:Cannot assign to 'self' outside of a method in the init
- [ios]关于error:Cannot assign to 'self' outside of a method in the init family
- 关于error:Cannot assign to 'self' outside of a method in the init family
- 关于error:Cannot assign to 'self' outside of a method in the init family
- c语言中左移问题
- 笔试题检验括弧的匹配是否正确 (){}【】《》
- 无效的主机/绑定变量名
- 网站主机安全之服务器的物理安全
- IntelliJ IDEA 14.0 javaweb项目发布及启动
- Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for
- 无符号数向有符号数自动转化问题
- Struts2:详解配置文件struts.xml
- Android中Activity之间切换动画的修改
- 图像局部特征学习(笔记1之具有尺度不变性的Harris角点)
- Hibernate 4.0+MySql5.5 无法自动创表
- 针对ArcGIS Server 跨域问题的解释
- AS边学边写——day01序章和开发前的准备
- 动态规划之LIS && ZigZag && BadNeighbors