Python 3.X: nonlocal support in eval/exec?

Terry Reedy tjreedy at udel.edu
Thu Aug 11 09:48:28 CEST 2011


On 8/11/2011 3:19 AM, Paddy wrote:
> We can access nonlocal variables in a function, but if we were to eval/
> exec the function we cannot set up a nested stack of evironment dicts.
> We are limited to just two: global and local.

Right. That was and is Python's execution model.
Note that when you exec code, including a function call, the locals 
passed is the local context in which the code is executed. It is not the 
locals of any particular function called by the exec-ed code.

If you exec a function that is a closure, the closure or non-local 
objects come with the function. A 'stack of dicts' has nothing to do 
with how function and nested funcs operate.

-- 
Terry Jan Reedy




More information about the Python-list mailing list