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


Groups > comp.lang.python > #19748

Re: Question about name scope

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"if': 0.04; 'exec': 0.07; 'dict': 0.09; 'doing,': 0.09; 'sure,': 0.09; 'variables.': 0.09; 'def': 0.13; 'argument': 0.15; 'cc:addr:python-list': 0.15; 'received:74.125.82.44': 0.15; 'received:mail-ww0-f44.google.com': 0.15; '42,': 0.16; 'camp': 0.16; 'evaluates': 0.16; 'sync': 0.16; 'wrote:': 0.16; 'wed,': 0.17; '>>>': 0.18; 'subject:Question': 0.19; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'pm,': 0.26; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'none,': 0.30; 'updated': 0.32; 'actually': 0.32; 'actual': 0.32; 'calling': 0.34; 'received:74.125.82': 0.34; '...': 0.35; 'cc:2**1': 0.36; 'none': 0.36; 'two': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'point': 0.39; 'subject:: ': 0.39; 'your': 0.61; 'results': 0.64; 'subject:name': 0.67; 'become': 0.69; 'dict,': 0.84; '-->': 0.91; 'updated.': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=swibVgCSfR0tTDIoh5htL2MsgWyoFq27Z3B12UgMN7M=; b=jqXJQSgIkiq4ZV5uv2DparZIwe+XJZ53XqfXGS+t2ZweGtOxDBCYdgvauYuMfjIB+2 ak33MU1/K+fF9MjEWDEsZlp7+GXqZKd36X9nm0MbvLeYmse3O7EWNY5WKD/pZ2D52p3c CphWuq2GF/ABtiqjQB0X0gJTP1y2c/jKNYhUo=
MIME-Version 1.0
In-Reply-To <4F29C255.1050009@stoneleaf.us>
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> <CALwzid=qdawuq7qd2Qyj9xR1jUo-KLhYMKDwLxDHX06ZJ8aDOw@mail.gmail.com> <4F29C255.1050009@stoneleaf.us>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 1 Feb 2012 16:00:02 -0700
Subject Re: Question about name scope
To Ethan Furman <ethan@stoneleaf.us>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, mwilson@the-wire.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5329.1328137234.27778.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328137234 news.xs4all.nl 6943 [2001:888:2000:d::a6]:59842
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19748

Show key headers only | View raw


On Wed, Feb 1, 2012 at 3:53 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> --> def f(x, y):
>
> ...     locals()[x] = y
> ...     print(vars())
> ...     exec('print (' + x + ')')
> ...     print(x)
> ...
> --> f('a', 42)
>
> {'y': 42, 'x': 'a', 'a': 42}
> 42
> a
>
> Indeed -- the point to keep in mind is that locals() can become out of sync
> with the functions actual variables.  Definitely falls in the camp of "if
> you don't know *exactly* what you are doing, do not play this way!"

Sure, but that's not actually out of sync.  The argument of your exec
evaluates to 'print (a)'.  You get two different results because
you're actually printing two different variables.  You can get the
dict temporarily out of sync:

>>> def f(x, y):
...     frob = None
...     loc = locals()
...     loc[x] = y
...     print(loc)
...     print(locals())
...     print(loc)
...
>>> f('frob', 42)
{'y': 42, 'x': 'frob', 'frob': 42, 'loc': {...}}
{'y': 42, 'x': 'frob', 'frob': None, 'loc': {...}}
{'y': 42, 'x': 'frob', 'frob': None, 'loc': {...}}

In this case, 'frob' is updated to 42 in the dict, but the optimized
local is not updated.  Calling locals() again refreshes the dict.

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