Writing func_closure?

Michael Hoffman cam.ac.uk at mh391.invalid
Thu Jun 9 04:00:04 EDT 2005


Fernando Perez wrote:

> Yes, I knew of the new.function() approach, but the problem is that I don't know
> how to make a fresh closure for it.  I can reuse the closure from a different
> function, but the docs don't say how to make a valid closure tuple.

 >>> def makeclosure(x):
...     def _f():
...         x
...     return _f.func_closure
...
 >>> makeclosure(42)
(<cell at 0x4613bc: int object at 0x4a3e4c>,)

OK, that's kind of limited. God only knows what happens when there is 
more than one variable, although you could add extra levels of hackery 
to do with that.

> Many thanks though, I'll probably end up using a less dirty hack :)

*Excellent* idea.
-- 
Michael Hoffman



More information about the Python-list mailing list