Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19746
| References | <20120201181117.5d35dddc@bigfoot.com> <mailman.5311.1328117874.27778.python-list@python.org> <jgc1cr$976$1@speranza.aioe.org> <CALwzidmBvCmeMOiSjOscuiTx7VXVmMvWkBU7hwQW5JDV85+N4A@mail.gmail.com> <4F29BB9C.70405@stoneleaf.us> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-02-01 15:38 -0700 |
| Subject | Re: Question about name scope |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5327.1328135930.27778.python-list@python.org> (permalink) |
On Wed, Feb 1, 2012 at 3:24 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> Definitely should rely on it, because in CPython 3 exec does not un-optimize
> the function and assigning to locals() will not actually change the
> functions variables.
Well, the former is not surprising, since exec was changed from a
statement to a built-in. I don't see any difference in the way
locals() behaves, though:
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(x, y):
... locals()[x] = y
... print(vars())
... exec('print(' + x + ')')
...
>>> f('a', 42)
{'y': 42, 'x': 'a', 'a': 42}
42
That still seems to work as I described it. You couldn't directly
reference it as 'a', though, since the result would be either that it
would try to look up a global with that name, or the compiler would
consider it a local, optimize it, and then you could no longer assign
it via locals().
Cheers,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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