Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11190 > unrolled thread

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

Started byPaddy <paddy3118@googlemail.com>
First post2011-08-11 00:19 -0700
Last post2011-08-11 13:31 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Python 3.X: nonlocal support in eval/exec? Paddy <paddy3118@googlemail.com> - 2011-08-11 00:19 -0700
    Re: Python 3.X: nonlocal support in eval/exec? Terry Reedy <tjreedy@udel.edu> - 2011-08-11 03:48 -0400
      Re: Python 3.X: nonlocal support in eval/exec? Paddy <paddy3118@googlemail.com> - 2011-08-11 13:31 -0700

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

FromPaddy <paddy3118@googlemail.com>
Date2011-08-11 00:19 -0700
SubjectPython 3.X: nonlocal support in eval/exec?
Message-ID<6392cd6f-5c11-4abb-91f4-5ca37baffc68@e35g2000yqc.googlegroups.com>
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.

[toc] | [next] | [standalone]


#11192

FromTerry Reedy <tjreedy@udel.edu>
Date2011-08-11 03:48 -0400
Message-ID<mailman.2158.1313048932.1164.python-list@python.org>
In reply to#11190
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

[toc] | [prev] | [next] | [standalone]


#11226

FromPaddy <paddy3118@googlemail.com>
Date2011-08-11 13:31 -0700
Message-ID<996e6195-1550-4149-ba5c-5f43eceebbad@fv14g2000vbb.googlegroups.com>
In reply to#11192
On Aug 11, 8:48 am, Terry Reedy <tjre...@udel.edu> wrote:
> 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

Thanks Terry.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web