Simple metaclass code failing

LittleGrasshopper seattlehanks at yahoo.com
Sun May 31 22:11:59 EDT 2009


On May 31, 4:11 pm, LittleGrasshopper <seattleha... at yahoo.com> wrote:
> On May 31, 3:59 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>
>
>
> > On May 31, 3:52 pm, LittleGrasshopper <seattleha... at yahoo.com> wrote:
>
> > > This is some simple code which I got from Guido's paper on the
> > > unification of classes and types, which Arnaud suggested to improve my
> > > knowledge of metaclasses:
>
> > > class M1(type):
> > >     pass
> > > class M2(M1):
> > >     pass
> > > class M3(M2):
> > >     pass
> > > class C1:
> > >     __metaclass__ = M1
> > > class C2(C1):
> > >     __metaclass__ = M2
> > > class C3(C1, C2):
> > >     __metaclass__ = M3
>
> > > It is failing when processing C3:
> > > Traceback (most recent call last):
> > >   File "metaerror.py", line 18, in <module>
> > >     class C3(C1, C2):
> > > TypeError: Error when calling the metaclass bases
> > >     Cannot create a consistent method resolution
> > > order (MRO) for bases C2, C1
>
> > > Since M3 is a subclass of M1 and M2 (which are the metaclasses for the
> > > bases of C3, I don't understand the error. Then again, the error seems
> > > to reference the MRO.
>
> > > I hope this is one of those posts that I can look back in a few months
> > > from now and think "Man, was that a dumb thing to ask!" Until then, I
> > > would appreciate your help in trying to figure out what's going on.
>
> >http://www.python.org/download/releases/2.3/mro/
>
> > Carl Banks
>
> Thank you, I'll read on that paper too in order to figure out what is
> going on. I guess the resulting MROs do not satisfy monotonicity, I
> just have to find out how to derive the MRO. I had the notion that it
> was constructed by listing the current class, followed by the MROs of
> each base in order, and then retaining the rightmost instance of each
> class in the MRO list, but I think that might be an
> oversimplification, since in this case that would be:
> (C1, object)
> (C2, C1, object)
> (C3, C2, C1, object)
> And I don't see any issues. But I'll read the paper to figure out what
> the problem is, thanks.

Thank you for pointing that paper. I read the paper and figured out
what was wrong (local precedence is not maintained.)



More information about the Python-list mailing list