python mro文件_Python MRO
文中代碼基于Python3.7
對于Python中的多繼承情況,運行時在搜索對象的屬性或方法時,需要遵循一定的順序規則,這個規則稱為:Method Resolution Order (MRO).
MRO規則可以總結為以下三句話:
In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching the same class twice.
So, first it goes to super class (and its super classes) given first in the list then second super class (and its super classes) , from left to right order. Then finally Object class, which is a super class for all classes.
這里的list指的是多個父類組成的list,如:
class M(X,Y,Z):
pass
list就是(X,Y,Z)
When in MRO we have a super class before subclass then it must be removed from that position in MRO.
這一句和第一句對應起來看,一個類只被檢索一次,所以基類要往后移
可以調用類型對象的mro方法或者__mro__屬性來獲取類型的MRO信息。
classX:defhello(self):print('x')
?
?classY:defhello(self):print('y')defworld(self):print('y_world')
?
?classZ:defhello(self):print('z')
?
?classA(X):defhello(self):print('a')
?
?classB(Y,Z):defhello(self):print('b')
?
?classM(B, A):pass?print(M.mro())print(M.__mro__)
?#輸出:#list類型
[, , , , , , ]#tuple類型
(, , , , , , )
MRO圖示如下:
goes to super class (and its super classes) given first in the list then second super class (and its super classes) , from left to right order. Then finally Object class
depth-first, left-right fashion without searching the same class twice,得到MRO列表:[, , , , , , ]
B和A均繼承自Z,M繼承自B和A:
classX:defhello(self):print('x')
?
?classY:defhello(self):print('y')defworld(self):print('y_world')
?
?classZ:defhello(self):print('z')
?
?classA(X,Z):defhello(self):print('a')
?
?classB(Y,Z):defhello(self):print('b')
?
?classM(B, A):pass?print(M.mro())
?#輸出:#[, , , , , , ]
MRO圖示如下:
goes to super class (and its super classes) given first in the list then second super class (and its super classes) , from left to right order. Then finally Object class
depth-first, left-right fashion without searching the same class twice,得到MRO列表:[, , , , , , ]
classX:defhello(self):print('x')
?
?classY:defhello(self):print('y')defworld(self):print('y_world')
?
?classZ:defhello(self):print('z')
?
?classA(X,Z):defhello(self):print('a')
?
?classB(Y,Z):defhello(self):print('b')
?
?classM(B, A, Y):pass?print(M.mro())
?#輸出#[, , , , , , ]
MRO圖示如下:
goes to super class (and its super classes) given first in the list then second super class (and its super classes) , from left to right order. Then finally Object class
這個MRO圖可以繼續簡化:
depth-first, left-right fashion without searching the same class twice
得到MRO列表為[, , , , , , ]
When in MRO we have a super class before subclass then it must be removed from that position in MRO
下面是一個會報錯的示例:
classA:defprocess(self):print('A process()')
?
?classB(A):defprocess(self):print('B process()')
?
?classM(A, B):pass?print(M.mro())
?#輸出:#TypeError: Cannot create a consistent method resolution#order (MRO) for bases A, B
MRO圖示:
如果一個方法或屬性同時存在與B和A,應為M直接繼承B又直接繼承A,那么通過M來調用時就不知道是該從B中還是A中獲取這個方法或屬性了,干脆就報錯吧。我覺得MRO順序應該為:M->B->A->object。
推薦閱讀
總結
以上是生活随笔為你收集整理的python mro文件_Python MRO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab repmat函数_Matl
- 下一篇: python e_pythone函数基础