Python's method-resolution algorithm: An implementation question

Ian Kelly ian.g.kelly at gmail.com
Tue Aug 15 15:41:32 EDT 2017


On Tue, Aug 15, 2017 at 12:57 PM, Evan Aad <oddeveneven at gmail.com> wrote:
> I don't see how, since the L(B*)'s are listed in order in the argument
> list: L(B1), L(B2), ..., and each L(B*) starts with B*: L(B1) = <B1,
> ...>, L(B2) = <B2, ...>, ...
>
> Could you please give a counter-example?

Sure.

merge(<B1, A1>, <B2, A2, B1>)  ->  <B2, B1, A1, A2>

vs:

merge(<B1, A1>, <B2, A2, B1>, <B1, B2>)  ->  TypeError

Or in Python:

>>> class A1: pass
>>> class A2: pass
>>> class B1(A1): pass
>>> class B2(A2, B1): pass
>>> class C(B1, B2): pass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot create a consistent method resolution order (MRO)
for bases B1, B2



More information about the Python-list mailing list