Created
November 14, 2018 17:47
-
-
Save huangsam/88497a1b9c50fb05aedddb293b123f35 to your computer and use it in GitHub Desktop.
Method Resolution Order in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Case 1: B, C are connected to A | |
""" | |
class A(object): pass | |
class B(A): pass | |
class C(A): pass | |
class D(B, C): pass | |
assert D.__mro__ == (D, B, C, A, object) | |
""" | |
Case 2: G, H have similar orderings | |
""" | |
class E(object): pass | |
class F(object): pass | |
class G(F, E): pass | |
class H(F, E): pass | |
class I(G, H): pass | |
assert I.__mro__ == (I, G, H, F, E, object) | |
""" | |
Case 3: L, M have bad orderings | |
""" | |
class J(object): pass | |
class K(object): pass | |
class L(J, K): pass | |
class M(K, J): pass | |
try: | |
class N(L, M): pass | |
except TypeError: | |
print('Cannot get MRO for (J, K) and (K, J)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment