Strange infinite recursion in metaclass's __new__

Jp Calderone exarkun at intarweb.us
Mon Nov 10 12:45:53 EST 2003


  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





More information about the Python-list mailing list