Finding closures through introspection

John Nagle nagle at animats.com
Tue Jun 15 01:58:23 EDT 2010


On 6/14/2010 9:33 PM, Steven D'Aprano wrote:
> 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:
>
> although this doesn't seem to be documented, at least not here:
>
> http://docs.python.org/reference/datamodel.html

    Got it. Check

	f.func_closure

for a non-null value.  For a closure, the value will be a Cell object.

The value of "func_closure" in f.func_globals is None, but that's the
wrong place to look, apparently.

				John Nagle



More information about the Python-list mailing list