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: 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> <4F29BB9C.70405@stoneleaf.us> <4F29C255.1050009@stoneleaf.us> From: Ian Kelly Date: Wed, 1 Feb 2012 16:00:02 -0700 Subject: Re: Question about name scope To: Ethan Furman 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Feb 1, 2012 at 3:53 PM, Ethan Furman wrote: > --> def f(x, y): > > ... =A0 =A0 locals()[x] =3D y > ... =A0 =A0 print(vars()) > ... =A0 =A0 exec('print (' + x + ')') > ... =A0 =A0 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 sy= nc > with the functions actual variables. =A0Definitely 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 =3D None ... loc =3D locals() ... loc[x] =3D 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.