Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

41 views
Skip to first unread message

Paddy

unread,
Aug 11, 2011, 3:19:34 AM8/11/11
to
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.

How about eval/exec take a new env argument that is a nested
dictionary whose outer level corresponds to locals, and which has a
__up__ value which is the next outer level environment dict. The
outermost level of the nested dictionary would correspond to the
global level?

I haven't given much thought to my suggested solution - I just didn't
want to leave a possible solution out. The major point is that there
seems to be no support for nonlocal in eval/exec (unless, trivially,
nonlocal==global).

- Paddy.

Terry Reedy

unread,
Aug 11, 2011, 3:48:28 AM8/11/11
to pytho...@python.org
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

Paddy

unread,
Aug 11, 2011, 4:31:45 PM8/11/11
to

Thanks Terry.

0 new messages