[Python-ideas] MRO local precedence ordering revisited

Stephan Sahm Stephan.Sahm at gmx.de
Thu Dec 10 11:36:15 EST 2015


Dear all,

I just pushed a very specific question which as I now recognized is far
more general: It concerns python MRO (method resolution order).

As reference I take https://www.python.org/download/releases/2.3/mro/
There the section "Bad Method Resolution Orders" starts with an example
which behaviour in the current python 2.7 is rather unintuitive to me.

I rename the example slightly to illustrate its usecase.

class Mixin(object):
>     pass

class A(Mixin):
>     pass
> class B(Mixin, A):
>    pass


this unfortunately throws "TypeError: Error when calling the metaclass
bases Cannot create a consistent method resolution order (MRO) for bases A,
Mixin"


The reference name above​ comments this case similar to the following
(adapted to the easier example above):

> We see that class
> ​B​
>  inherits from
> ​Mixin​
>  and
> ​A​
> , with
> ​Mixin
>  before
> ​A​
> : therefore we would expect attribute
> ​s of ​
> ​B
>  to be inherited
> ​ first​
> by
> ​Mixin
>  and
> ​then​
> by
> ​A
> : nevertheless Python 2.2
> ​was giving the opposite behaviour.
>
​...
> As a general rule, hierarchies such as the previous one should be avoided,
> since it is unclear if
> ​Mixin
>  should override
> ​A​
>  or viceversa​



​While it might be the case that in Python​ 2.2 things where different, I
cannot agree that the expected order of Method resolution is ambiguous (at
least as far I see at this stage). The reference itself says that we would
expect
B
 to be inherited
​ first​
 by
​Mixin
 and
​then​
by
​A. There is no ambiguity any longer.

*Therefore I would like to propose to make this MRO again a valid one.*


The usecase should be obvious: If you want a Mixin to be the first thing
overwriting functions, but still want to inherit as normal.
(I myself want for instance a Mixin to overwrite the __init__ method, which
is simply not impossible when choosing class B(A, Mixin))


best,
Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151210/731e806c/attachment-0001.html>


More information about the Python-ideas mailing list