`
ooaer
  • 浏览: 137479 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

(转)Method Resolution Order (MRO) in new style Python classes

阅读更多

Stack Overflow link: http://stackoverflow.com/questions/1848474/method-resolution-order-mro-in-new-style-python-classes

The crucial difference between resolution order for legacy vs new-style classes comes when the same ancestor class occurs more than once in the "naive", depth-first approach -- e.g., consider a "diamond inheritance" case:

>>> class A: x = 'a'
... 
>>> class B(A): pass
... 
>>> class C(A): x = 'c'
... 
>>> class D(B, C): pass
... 
>>> D.x
'a'


here, legacy-style, the resolution order is D - B - A - C - A : so when looking up D.x, A is the first base in resolution order to solve it, thereby hiding the definition in C. While:

>>> class A(object): x = 'a'
... 
>>> class B(A): pass
... 
>>> class C(A): x = 'c'
... 
>>> class D(B, C): pass
... 
>>> D.x
'c'
>>>



here, new-style, the order is:

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



with A forced to come in resolution order only once and after all of its subclasses, so that overrides (i.e., C's override of member x) actually work sensibly.

It's one of the reasons that old-style classes should be avoided: multiple inheritance with "diamond-like" patterns just doesn't work sensibly with them, while it does with new-style.

 

分享到:
评论

相关推荐

    python:MRO的计算方式

    我们都已经知道,Python 3(Python 2 的新式类)中多继承模式是使用 C3 算法来确定 MRO(Method Resolution Order) 的。 下面就讲解C3算法具体是怎么计算的。 MRO计算规则 首先来定义一些符号: 用CN表示一个类:C1, C2...

    理解Python的多继承MRO

    Method Resolution Order , 定义了Python中多继承存在的情况下,解释器查找函数解析的具体顺序 什么是函数解析顺序 # 经典继承问题 - 棱形继承 class A: def who_am_i(self): print("i am A") class B: pass ...

    浅谈Python的方法解析顺序(MRO)

    方法解析顺序, Method Resolution Order 从一段代码开始 考虑下面的情况: class A(object): def foo(self): print('A.foo()') class B(object): def foo(self): print('B.foo()') class C(B, A): pass c = C()...

    Python 多继承类的 MRO 算法——C3算法

    Python2.3之后对于多继承类计算父类的搜索顺序时采用了新的算法,c3算法。这里是c3的源码以及测试脚本,希望能有所帮助

    Python 的描述符 descriptor详解

    Python 在 2.2 版本中引入了descriptor(描述符)功能,也正是基于这个功能实现了新式类(new-styel class)的对象模型,同时解决了之前版本中经典类 (classic class) 系统中出现的多重继承中的 MRO(Method Resolution...

    Python 107.mro().mp4

    Python 107.mro().mp4

    Python库 | python-mro-language-server-0.0.1.tar.gz

    python库。 资源全名:python-mro-language-server-0.0.1.tar.gz

    Python多继承原理与用法示例

    MRO即method resolution order,用于判断子类调用的属性来自于哪个父类。在Python2.3之前,MRO是基于深度优先算法的,自2.3开始使用C3算法,定义类时需要继承object,这样的类称为新式类,否则为旧式类 从图中可以...

    python多重继承新算法C3介绍

    mro即 method resolution order (方法解释顺序),主要用于在多继承时判断属性的路径(来自于哪个类)。 在python2.2版本中,算法基本思想是根据每个祖先类的继承结构,编译出一张列表,包括搜索到的类,按策略删除重复...

    Python中super()函数简介及用法分享

    首先看一下super()函数的定义: super([type [,object-or-type]]) Return a **proxy object** that ...第一个参数的__mro__属性决定了搜索的顺序, super指的的是 MRO(Method Resolution Order) 中的下一个类, 而不一定是

    Python多继承以及MRO顺序的使用

    主要介绍了Python多继承以及MRO顺序的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    圣诞树代码编程python-24-拓展-mro顺序.ev4.rar

    圣诞树代码编程python-24-拓展_mro顺序.ev4.rar

    煤矿设备MRO支持系统设计与实现

    煤矿企业规模的不断扩大、煤矿设备的日益复杂以及企业信息交互对象持续增加,使煤矿设备MRO管理变得越来越复杂。基于此背景,以设备维修维护为重点,分析了煤矿设备MRO典型业务需求,给出了煤矿设备MRO支持系统功能模块...

    python 零基础学习篇python课程django框架 django会话保持和视图12 MRO的继承顺序 .mp4

    python 零基础学习篇

    Python库 | mro_tools-0.1.2-py2.py3-none-any.whl

    python库。 资源全名:mro_tools-0.1.2-py2.py3-none-any.whl

    Python库 | mro_tools-0.2.0-py2.py3-none-any.whl

    python库。 资源全名:mro_tools-0.2.0-py2.py3-none-any.whl

    中国MRO使用手册

    MRO技术的研究和应用已经走出了传统的以生产物料供应为特征的模式,已全面覆盖产品全生命周期的中间阶段。随着MRO技术研究不断深入,MRO技术的应用范围已经延伸到例如煤炭、化工等原材料生产领域,为MRO技术的发展...

Global site tag (gtag.js) - Google Analytics