Michele Simionato wrote: <snip>
There is a minor bug in your code: > def thisclass(proc, *args, **kw): > """ Example: > >>> def register(cls): print 'registered' > ... > >>> class C: > ... thisclass(register) > ... > registered > """ > # basic idea stolen from zope.interface, which credits P.J. Eby > frame = sys._getframe(1) > assert '__module__' in frame.f_locals # > <----------------------------------- here > def makecls(name, bases, dic): > try: > cls = type(name, bases, dic) > except TypeError, e: > if "can't have only classic bases" in str(e): > cls = type(name, bases + (object,), dic) > else: # other strange errors, such as __slots__ conflicts, etc > raise > del cls.__metaclass__ > proc(cls, *args, **kw) > return cls > frame.f_locals["__metaclass__"] = makecls > > Figured you would like this one ;) > > Michele Simionato See this example: >>> import sys >>> def in_class_statement1(): ... frame = sys._getframe(1) ... return '__module__' in frame.f_locals ... >>> def in_class_statement2(): ... frame = sys._getframe(1) ... return '__module__' in frame.f_locals and not \ ... '__module__' in frame.f_code.co_varnames ... >>> class A(object): ... print in_class_statement1() ... print in_class_statement2() ... True True >>> def f(): ... __module__ = 1 ... print in_class_statement1() ... print in_class_statement2() ... >>> f() True False -- http://mail.python.org/mailman/listinfo/python-list