Re: Finding closures through introspection

2010-06-14 Thread John Nagle
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: htt

Re: Finding closures through introspection

2010-06-14 Thread Ian Kelly
On Mon, Jun 14, 2010 at 9:46 PM, John Nagle wrote: > No indication there that "fbar" is a closure inside "foo".  There > are "__closure__" and "__func_closure__" entries in there, but > they are both None.  A non-closure function has the same entries. > > So how can I detect a closure? Maybe beca

Re: Finding closures through introspection

2010-06-14 Thread Steven D'Aprano
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(

Finding closures through introspection

2010-06-14 Thread John Nagle
I'm doing something with CPython introspection, and I'm trying to determine whether a function is a closure. Consider def foo(x) : global fbar def bar(y) : pass fbar = bar # export closure foo(0) We now have "fbar" as a reference to a closure. "inspect" can tell u