On Nov 8, 2007 5:30 PM, Prepscius, Colin (IT) <[EMAIL PROTECTED]> wrote: > The last argument to new.function takes a closure, which is a tuple of > cell objects. Does anybody know how to create those cell objects 'by > hand'? >
Beyond copying them from an existing closure, you'll have to use the C API to create the new cell objects: >>> pc = ctypes.pythonapi.PyCell_New >>> pc.restype = ctypes.py_object >>> pc.argtypes = [ctypes.py_object] >>> pc(100) <cell at 0x01B8D970: int object at 0x00946044> >>> import new >>> def f(): ... x = 10 ... def g(): ... return x ... return g ... >>> f() <function g at 0x01B8CF30> >>> g = _ >>> new.function(g.func_code, {}, "freeeeee as a bird", None, (pc(100),)) <function freeeeee as a bird at 0x01B941F0> >>> _() 100 >>> g() 10 I don't really know how safe this is or what sort of refcount or GC implications creating cell objects on the fly like this might have. -- http://mail.python.org/mailman/listinfo/python-list