Dave Kuhlman wrote:
> If there were really such a thing as nested
> scopes/namespaces, we would have a function that would give us
> access to them, similar to the way that locals() and globals()
> give us access to the local and global namespace.
>   
Nested namespaces are actually stored with the nested function. They are 
also called closures. For example:

In [1]: def m(x):
   ...:     y=3
   ...:     def f(z):
   ...:         print x, y, z
   ...:     return f
   ...:

In [2]: ff=m(2)

Just to prove that the closure exists:
In [3]: ff(5)
2 3 5

The closure is stored in the func_closure attribute of the function:
In [5]: ff.func_closure
Out[5]:
(<cell at 0x00E39170: int object at 0x00A658D8>,
 <cell at 0x00E39150: int object at 0x00A658E4>)

In [6]: ff.func_closure[0]
Out[6]: <cell at 0x00E39170: int object at 0x00A658D8>

This is pretty opaque. There is a way to get the values out; see this 
recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439096

For more discussion, search comp.lang.python for func_closure.

Kent

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to