[英]Trying to understand Python MRO, but confused with its resolution order
So I was trying to understand how the MRO works and I unable to understand some parts of it, I understand that the interpreter goes left to right and chooses specificity over generics, if so then how does this happen?所以我试图了解 MRO 是如何工作的,但我无法理解其中的某些部分,我知道解释器从左到右并选择 generics 的特异性,如果是这样,那么这是怎么发生的?
class X:
pass
class Y:
pass
class Z:
pass
class w:
pass
class A(X, Y):
pass
class B(w, Z):
pass
class D(X, w):
pass
class M(D, A, B, Z):
pass
print(M.mro())
# Output
# [<class '__main__.M'>, <class '__main__.D'>, <class '__main__.A'>, <class '__main__.X'>,
# <class '__main__.Y'>, <class '__main__.B'>, <class '__main__.w'>, <class '__main__.Z'>,
# <class 'object'>]
or this或这个
class X:
pass
class Y:
pass
class Z:
pass
class A(X, Y):
pass
class B(Y, Z):
pass
class M(B, A, Z):
pass
# Output:
# [<class '__main__.M'>, <class '__main__.B'>,
# <class '__main__.A'>, <class '__main__.X'>,
# <class '__main__.Y'>, <class '__main__.Z'>,
# <class 'object'>]
print(M.mro())
Could someone help me understand the resolution order, because to me it feels like the rules are contradicting each other, each time the inheritance level get a bit more complex.有人可以帮我理解解决顺序,因为对我来说,每次 inheritance 级别变得更复杂时,我感觉规则是相互矛盾的。
The MRO follows from two simple rules: MRO 遵循两个简单的规则:
Consider M
.考虑
M
。 By rule 1, M
must occur before B
, A
, and Z
.根据规则 1,
M
必须出现在B
、 A
和Z
之前。 By rule 2, B
must come before A
, and A
must come before Z
.根据规则 2,
B
必须在A
之前,并且A
必须在Z
之前。
Note that it isn't always possible to find an MRO that obeys these rules, in which case you'll get an error immediately:请注意,并非总是可以找到遵守这些规则的 MRO,在这种情况下,您会立即收到错误消息:
>>> class X: pass
...
>>> class Y: pass
...
>>> class A(X, Y): pass
...
>>> class B(Y, X): pass
...
>>> class C(A, B): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Cannot create a consistent method resolution
order (MRO) for bases X, Y
A
and B
are fine, but no MRO can be found for C
. A
和B
都很好,但找不到C
的 MRO。 In C
's MRO, X
would have to precede Y
since C
inherits from A
.在
C
的 MRO 中, X
必须在Y
之前,因为C
继承自A
But because C
also inherits from B
, Y
must precede X
.但是因为
C
也继承自B
,所以Y
必须在X
之前。 There's no ordering that satisfies both constraints.没有满足这两个约束的排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.