How to get MRO in __new__() of metaclasses?

Frank von Delft fvondelft at syrrx.com
Tue Dec 10 20:56:55 EST 2002


> > I also slightly fail to understand why you can't just scan through the
> > immediate superclasses, but there may be something I've missed.
> 
> .... whereas this you're absolutely right about this - d'uh!!!  Thanks
> a stack, that was exactly what I was missing:  the (bases) tuple *is*
> the MRO of the about-to-be-created class, and there's no need to
> blunder about the superclasses as I was doing, *because* that gives
> the wrong MRO.

Actually, after trying it, it turns out that's not good enough.  I can
think of two ways to check whether one of the base classes has an
attribute:
      if hasattr(base,attr):
and 
      if attr in base.__dict__:

(I don't think "super()" is applicable.)  But base.__dict__ only
contains attributes defined in that class, none of those inherited. 
OTOH, hasattr() does scan through the whole inheritance tree, but only
for that base, so this is again different from the new-style MRO (in
particular for diamond-shaped inheritance).

I suspect I'll have to try to reproduce the new-style MRO manually,
and will probably get it wrong :-(

phx.



More information about the Python-list mailing list