Accessing nested functions for testing

Erik Max Francis max at alcyone.com
Tue May 20 03:08:52 EDT 2003


Chad Netzer wrote:

> def foo():
>     def bar():
>         pass
>     bar()
> 
> Is it possible to execute bar from outside of foo?  I can't even find
> a way to see or access bar at all, from the global scope.
> 
> My intuition says this is not possible, I'm just curious if that is
> truly the case.

I don't believe so, not without poking around through the bytecodes and
picking out the name and doing something with that.  Poking around with
the attributes in foo, I can see the name 'bar' (as
foo.func_code.co_varnames), but I don't think there's any way to get at
the inner function bar without magic.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ A father is a thousand schoolmasters.
\__/  Louis Nizer




More information about the Python-list mailing list