How to get MRO in __new__() of metaclasses?

Frank von Delft fvondelft at syrrx.com
Mon Dec 9 05:19:38 EST 2002


Hi all

Metaclasses rock!  One thing I didn't manage to find, however:  is
there an easy way to retrieve the MRO of the about-to-be-created class
while one is still mucking about in the __new__() method of its
metaclass?

Most metaclasses seem to have the form:

#-------------------------------------------
class SomeMetaclass(type):
   def __new__(cls,name,bases,dict):
      # ... do stuff to dict ... #
      return type.__new__(cls,name,bases,dict)
#-------------------------------------------

i.e., while still in __new__(), the new class (name) has not yet been
created, so its __mro__ attribute is obviously not available.  Is
there an easy way to get it, though, short of reimplementing the mro
algorithm?

I also tried doing my thang in __init__() instead, i.e. when the class
*has* been created and __mro__ therefore is available, but it seems
(??) not possible to change the __dict__ anymore once the class is
created.  (At least, I think that's what the errors were trying to
tell me...)

Any tips will be hugely appreciated.

Cheers
Phraenquex



More information about the Python-list mailing list