標籤:eal 順序 target details 繼承 base div 路徑 tail
mro即method resolution order,主要用於在多繼承時判斷調的屬性的路徑(來自於哪個類)。
http://blog.csdn.net/imzoer/article/details/8737642
你真的理解Python中MRO演算法嗎?
API:
1 class type(object): 2 """ 3 type(object) -> the object‘s type 4 type(name, bases, dict) -> a new type 5 """ 6 def mro(self): # real signature unknown; restored from __doc__ 7 """ 8 mro() -> list 9 return a type‘s method resolution order10 """11 return []
測試代碼:
1 #!/usr/bin/env python 2 class A(object): 3 def __init__(self): 4 print "enter A" 5 print("leave A") 6 7 8 class B(object): 9 def __init__(self):10 print "enter B"11 print "leave B"12 13 14 class C(A,B):15 def __init__(self):16 print "enter C"17 super(C, self).__init__()18 print "leave C"19 20 b = C()21 mo = b.__class__.mro()22 print mo
輸出:
enter C
enter A
leave A
leave C
[<class ‘__main__.C‘>, <class ‘__main__.A‘>, <class ‘__main__.B‘>, <type ‘object‘>]
Python的方法解析順序(MRO)