[Python-ideas] Consider (one day) adding an inheritance order class precedence mechanism

Neil Girdhar mistersheik at gmail.com
Mon Nov 20 20:09:44 EST 2017


On Sat, Nov 18, 2017 at 9:29 PM Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 19 November 2017 at 06:56, Neil Girdhar <mistersheik at gmail.com> wrote:
> > Would you mind explaining why it's necessary for C3 to complain?
> >
> > In:
> >
> > S < C
> > B < S, E
> > R < E, C
> > Z < B, R
> >
> > If Z is told to have MRO:
> >
> > (Z, B, S, R, E, C)
> >
> > then there are no conflicts with any base classes.
>
> I don't actually know what C3 allows in principle, I only know that
> CPython's resolver still complains in practice:
>
>     >>> class C: pass
>     ...
>     >>> class S(C): pass
>     ...
>     >>> class E: pass
>     ...
>     >>> class B(S, E): pass
>     ...
>     >>> class R(E, C): pass
>     ...
>     >>> class Z(B, S, R, E, C): pass
>     ...
>     Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>     TypeError: Cannot create a consistent method resolution order
> (MRO) for bases C, E
>
> I think the problem is that the resolver isn't looking at the declared
> bases of "B", and "R", it's looking at their full MRO:
>
>     >>> B.__mro__
>     (<class '__main__.B'>, <class '__main__.S'>, <class '__main__.C'>,
> <class '__main__.E'>, <class 'object'>)
>     >>> R.__mro__
>     (<class '__main__.R'>, <class '__main__.E'>, <class '__main__.C'>,
> <class 'object'>)
>
> Changing the heuristics used to generate B's MRO such that "C" and "E"
> appeared in the opposite order wouldn't really help, since that would
> just flip the problematic case to be the "R(C, E)" declaration.
>

Sorry, I wrote back too quickly.  I meant also to change B's requested MRO
to be:

(B, S, E, C)

It works with that change.


>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171121/97685a56/attachment.html>


More information about the Python-ideas mailing list