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


Groups > comp.lang.python > #19744

Re: Question about name scope

References <20120201181117.5d35dddc@bigfoot.com> <mailman.5311.1328117874.27778.python-list@python.org> <jgc1cr$976$1@speranza.aioe.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-02-01 14:49 -0700
Subject Re: Question about name scope
Newsgroups comp.lang.python
Message-ID <mailman.5325.1328133024.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Feb 1, 2012 at 11:47 AM, Mel Wilson <mwilson@the-wire.com> wrote:
> I guess they want local symbols in functions to be pre-compiled.  Similar to
> the way you can't usefully update the dict returned by locals().  Strangely,
> I notice that
>
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def f(x):
> ...   exec x
> ...   exec 'print a'
> ...
>>>> f('a=4')
> 4
>>>>
>
> works, but I really cannot explain why.

I am not a dev, but I believe it works because assigning to locals()
and assigning via exec are not the same thing.  The problem with
assigning to locals() is that you're fundamentally just setting a
value in a dictionary, and even though it happens to be the locals
dict for the stack frame, Python can't figure out that it should go
and update the value of the optimized local to match.  exec, on the
other hand, compiles and executes an actual STORE_NAME operation.  Of
course, if the particular local variable hasn't been optimized by the
compiler, then updating locals() works just fine (although you
probably should not rely on this):

>>> def f(x, y):
...     locals()[x] = y
...     print locals()[x]
...     exec 'print ' + x
...
>>> f('a', 42)
42
42

Another interesting thing to note is that the print in your example
doesn't even need to be in a second exec, which I believe works
because the presence of any exec statement disables global variable
optimizations for the function.  Compare:

>>> def f(x):
...     locals()['a'] = 4
...     print a
...
>>> f('pass')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in f
NameError: global name 'a' is not defined
>>> def f(x):
...     locals()['a'] = 4
...     print a
...     exec x
...
>>> f('pass')
4

And while we're on the subject, here's a nicely obscure syntax error:

>>> def f(x):
...   def g():
...     print x
...   exec x
...
  File "<stdin>", line 4
SyntaxError: unqualified exec is not allowed in function 'f' it
contains a nested function with free variables

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about name scope Olive <diolu@bigfoot.com> - 2012-02-01 18:11 +0100
  Re: Question about name scope Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-01 09:21 -0800
  Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 09:43 -0800
  Re: Question about name scope Dave Angel <d@davea.name> - 2012-02-01 12:36 -0500
    Re: Question about name scope Mel Wilson <mwilson@the-wire.com> - 2012-02-01 13:47 -0500
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 14:49 -0700
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 15:38 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:24 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:00 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:08 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:47 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:53 -0800
        Re: Question about name scope Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-02 00:34 +0000
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:59 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:41 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:51 -0800
  Re: Question about name scope Chris Rebert <clp2@rebertia.com> - 2012-02-01 09:38 -0800
  Re: Question about name scope Christian Heimes <lists@cheimes.de> - 2012-02-01 18:50 +0100

csiph-web