Python的方法解析顺序(MRO)

mro即method resolution order,主要用于在多继承时判断调的属性的路径(来自于哪个类)。

http://blog.csdn.net/imzoer/article/details/8737642

你真的理解Python中MRO算法吗?

API:

 class type(object):
"""
type(object) -> the object's type
type(name, bases, dict) -> a new type
"""
def mro(self): # real signature unknown; restored from __doc__
"""
mro() -> list
return a type's method resolution order
"""
return []

测试代码:

 #!/usr/bin/env python
class A(object):
def __init__(self):
print "enter A"
print("leave A") class B(object):
def __init__(self):
print "enter B"
print "leave B" class C(A,B):
def __init__(self):
print "enter C"
super(C, self).__init__()
print "leave C" b = C()
mo = b.__class__.mro()
print mo

输出:

enter C
enter A
leave A
leave C
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>]

上一篇:【转载】Android Studio Service AIDL 详解


下一篇:C++内存读写例子