Finding closures through introspection
Steven D'Aprano
steve-REMOVE-THIS at cybersource.com.au
Tue Jun 15 00:33:57 EDT 2010
On Mon, 14 Jun 2010 20:46:28 -0700, John Nagle wrote:
> So how can I detect a closure?
I *think* you do it through the co_flags attribute of the code object.
This is in Python 2.5:
>>> def f(x):
... def g():
... return x
... return g
...
>>>
>>> closure = f(42)
>>> closure()
42
>>> closure.func_code.co_flags
19
>>> f.func_code.co_flags
3
although this doesn't seem to be documented, at least not here:
http://docs.python.org/reference/datamodel.html
--
Steven
More information about the Python-list
mailing list