Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'executed': 0.07; 'variables.': 0.07; 'assigning': 0.09; 'dict': 0.09; 'excluding': 0.09; 'subject:Python3': 0.09; 'terry': 0.09; 'library': 0.15; "'),": 0.16; 'created.': 0.16; 'example)': 0.16; 'highlighted': 0.16; 'least,': 0.16; 'local.': 0.16; 'merely': 0.16; 'namespace,': 0.16; 'reedy': 0.16; 'top-level': 0.16; 'wrote:': 0.17; 'variables': 0.17; '>>>': 0.18; 'code,': 0.18; 'bit': 0.21; 'do.': 0.21; 'earlier': 0.21; 'not,': 0.21; 'assignment': 0.22; 'explicit': 0.22; 'statement': 0.23; 'idea': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'am,': 0.27; 'skip:e 30': 0.27; "doesn't": 0.28; 'fixed': 0.28; 'manual': 0.29; 'skip:_ 10': 0.29; 'that.': 0.30; 'returned': 0.30; 'function': 0.30; 'could': 0.32; 'function.': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'entry': 0.33; 'changed': 0.34; 'doing': 0.35; "won't": 0.35; 'but': 0.36; 'wanted': 0.36; 'does': 0.37; 'being': 0.37; '(for': 0.37; 'previous': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'nothing': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'claim': 0.60; 'kind': 0.61; 'back': 0.62; 'more': 0.63; 'within': 0.64; 'useful.': 0.65; 'restore': 0.69; 'received:74.208': 0.71; 'unusual': 0.71; 'subject:this': 0.84; 'dict.': 0.84; 'everything.': 0.84; 'locals': 0.84; 'presumably': 0.84; 'mean.': 0.91; 'angel': 0.93 Date: Tue, 12 Feb 2013 11:40:55 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python3 exec locals - this must be a FAQ References: <511A434D.4030309@davea.name> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:dzfw6OhqgcIcmI+XGt1KDMbQnuWtDL3aD75yPBWOy9e IiYyKdtI3PGYgM64VJj5SVgF+cWOme/TTggatU6S8mgemt/Kmg WoSVQJ4J6nUomVq6Qa4dEhE/eDDSOz6D4w8gJbD6IZfyj7dXW8 fz3JFCucbKV5Vlnz/9x7P0+6lWvW0TzeZg+ALDrKxaogNjU6dR YbsLuFq7AIe/EEC1XYi0mtyp6tFED2W9Nj7EIvCERNDcAMGcQj 5AYroq+f51I4sV9R5hcDO2+4fj0C6lAsraAJecfVzZTJQFB67L A9V4ERy7nFb6GwpK67W0VvFLwe0RJph1z88WGlLlc/Hy1Efxw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360687277 news.xs4all.nl 6904 [2001:888:2000:d::a6]:50059 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38770 On 02/12/2013 10:50 AM, Terry Reedy wrote: > On 2/12/2013 8:27 AM, Dave Angel wrote: >> On 02/12/2013 06:46 AM, Helmut Jarausch wrote: > >>> >>> >>> Doing >>> >>> _locals= locals() > > This merely gives you a handle of the dict returned by locals() for when > you want to do more than just pass it to (for example) exec). This is > unusual because it is not very useful. > >> This doesn't copy everything. The OP presumably wanted to restore the original values of the original variables. The above "assignment" won't help a bit with that. > > I have no idea what you mean. The locals() dict contains all local and > nonlocal names, excluding global names, which are in the globals() dict. > >>> expr=compile(input('statements assigning to some local variables '), >>> 'user input','exec') >>> exec(expr,globals(),_locals) >>> >>> How can I "copy" the new values within _locals to my current locals. >>> >>> If I knew that Var1 has changed I could say >>> Var1 = _locals['Var1'] but what to do in general? > > If you want to put a value back into the function local namespace, this > is the only thing you can do. In CPython, at least, all function local > names must be explicit and known when the function statement is executed > and the function object is created. Read the Library manual entry for > locals(), including the highlighted note. > >> locals()["Var1"] = _locals["Var1"] will set the same Var1 local. > > Huh??? The dict returned by this locals call is the same dict returned > by the previous locals call and bound to _locas. So the above does > nothing. It is the same thing as _locals["Var1"] = _locals["Var1"]. > My claim was based on the assumption that the earlier assignment had been fixed by some kind of copy. If not, there's nothing to restore. I also retracted my use of that trick anyway, since being corrected by MRAB. I only tested it in top-level code, not inside a function. -- DaveA