Strange infinite recursion in metaclass's __new__

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Mon Nov 10 20:35:58 CET 2003


Jp Calderone wrote:
>   Due to some bizarre constraints placed on me, I've written the following
> metaclass:
> 
>     import types
> 
>     def remove(t, o):
>         return tuple([e for e in t if t not in o])
> 
>     class BizarreMetaclass(type):
>         def __new__(klass, name, bases, attrs):
>             if name == 'BaseClass':
>                 return type.__new__(klass, name, bases, attrs)
>             return types.ClassType(name, remove(bases, [object]), attrs)
> 
>     class BaseClass(object):
>         __metaclass__ = BizarreMetaclass
> 
>     class Foo(BaseClass):
>         pass
> 
>   The solution is extremely hacky (but I've already tried and discarded
> about a dozen more straightforward attempted solutions).
> 
>   It is also broken, but it is broken in a way that I do not understand.
> 
>   Can anyone explain the unbounded recursion that occurs?
> 
>   Jp
> 

What's strange?

object isn't immediate parent of Foo. Therefore, you actually don't 
alter bases list :)

anton.





More information about the Python-list mailing list