[Python-ideas] MRO local precedence ordering revisited
Stephen J. Turnbull
stephen at xemacs.org
Thu Dec 10 13:46:58 EST 2015
On Fri, Dec 11, 2015 at 3:36 AM, Stephan Sahm <Stephan.Sahm at gmx.de> wrote:
> > 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).
It's unambiguous to you because you *already know* your "expected"
semantics. The spam-eggs example in Michele Simionato's paper that
was cited earlier (https://www.python.org/download/releases/2.3/mro/)
makes it clear why this is ambiguous from the point of view of the
interpreter. The interpreter can only see the syntax, and there are
two semantic principles it can follow to determine what various users
might expect: use the most specific base class first (which enforces
monotonicity IIUC), or use the order specified in the class first
(local precedence). In this case those two principles conflict, and
therefore MRO is ambiguous.
In cases where you know the "expected" semantics, probably you can use
a metaclass to enforce them (don't ask me how, I've never programmed a
metaclass).
BTW, multiple inheritance != mixin, and I certainly wouldn't call your
Liftable class (from the original thread) a mixin, as it has complex
and unobvious semantics of instance initialization in the presence of
multiple inheritance. YMMV, as there doesn't seem to be an official
definition of mixin for Python.
Chris Angelico writes:
> (At least, I don't _think_ the current behaviour could be
> considered a bug. I could be wrong.)
Given that this was discussed extensively for 2.3, and Guido found
himself compelled to change his mind to support the C3 algorithm by
Samuele Pedroni's examples at that time, and that the C3 algorithm has
been widely adopted by other languages (intuition portability!), I
would say, no, this is *not* a bug. There may be an algorithm which
satisfies the C3 conditions and generates a order in more cases, in
which case changing to that algorithm would be a reasonable feature
request. But C3 still excludes the OP's desired MRO algorithm.
More information about the Python-ideas
mailing list