Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!texta.sil.at!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exec': 0.07; 'executed': 0.07; 'variables.': 0.07; 'python': 0.09; 'assigning': 0.09; 'dict': 0.09; 'excluding': 0.09; 'modifies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Python3': 0.09; 'terry': 0.09; 'index': 0.13; 'library': 0.15; "'),": 0.16; 'created.': 0.16; 'example)': 0.16; 'function).': 0.16; 'highlighted': 0.16; 'least,': 0.16; 'local.': 0.16; 'merely': 0.16; 'namespace,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'jan': 0.18; 'windows': 0.19; 'changes': 0.20; 'do.': 0.21; 'explicit': 0.22; 'names.': 0.22; 'statement': 0.23; "i've": 0.23; 'idea': 0.24; 'pass': 0.25; 'tried': 0.25; 'least': 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; 'header:X-Complaints-To:1': 0.28; 'fine': 0.28; 'manual': 0.29; 'included': 0.29; 'skip:_ 10': 0.29; 'that.': 0.30; 'e.g.': 0.30; 'returned': 0.30; 'function': 0.30; 'could': 0.32; 'handle': 0.33; 'to:addr:python-list': 0.33; 'entry': 0.33; 'changed': 0.34; 'doing': 0.35; 'received:org': 0.36; 'but': 0.36; "didn't": 0.36; 'should': 0.36; 'does': 0.37; '(for': 0.37; 'previous': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'back': 0.62; 'more': 0.63; 'within': 0.64; 'useful.': 0.65; 'unusual': 0.71; 'transfer': 0.76; 'subject:this': 0.84; 'dict.': 0.84; 'everything.': 0.84; 'locals': 0.84; 'manual,': 0.84; 'received:fios.verizon.net': 0.84; 'mean.': 0.91; 'net.': 0.91; 'angel': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python3 exec locals - this must be a FAQ Date: Tue, 12 Feb 2013 10:50:11 -0500 References: <511A434D.4030309@davea.name> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <511A434D.4030309@davea.name> 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: 1360684232 news.xs4all.nl 6969 [2001:888:2000:d::a6]:43507 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38765 On 2/12/2013 8:27 AM, Dave Angel wrote: > On 02/12/2013 06:46 AM, Helmut Jarausch wrote: >> I've tried but didn't find an answer on the net. You should start with the fine manual, which is on the net as well as included with at least the Windows distribution. It has a nice index that includes an entry for locals (built-in function). >> The exec function in Python modifies a copy of locals() only. >> How can I transfer changes in that local copy to the locals of my >> function >> ** without ** knowing the names of these variables. You cannot. Just accept that. >> E.g. I have a lot of local names. >> >> 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. 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"]. -- Terry Jan Reedy