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


Groups > comp.lang.python > #38756

Re: Python3 exec locals - this must be a FAQ

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; '(at': 0.03; 'exec': 0.07; 'raises': 0.07; 'variables.': 0.07; 'python': 0.09; '"a"': 0.09; '(it': 0.09; 'assigning': 0.09; 'modifies': 0.09; 'nameerror:': 0.09; 'subject:Python3': 0.09; '{},': 0.09; 'def': 0.10; '"b"': 0.16; "'),": 0.16; "'b'": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'ignored,': 0.16; 'local.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'syntax.': 0.16; "{'a':": 0.16; 'wrote:': 0.17; 'skip:{ 20': 0.17; 'variables': 0.17; '>>>': 0.18; 'variable': 0.20; 'changes': 0.20; '"",': 0.22; 'names.': 0.22; 'defined': 0.22; 'example': 0.23; "i've": 0.23; 'testing': 0.24; 'tried': 0.25; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; '(most': 0.27; 'am,': 0.27; 'implemented': 0.27; 'skip:e 30': 0.27; "doesn't": 0.28; 'attempting': 0.29; 'dictionary': 0.29; 'skip:_ 10': 0.29; 'e.g.': 0.30; 'function': 0.30; 'file': 0.32; 'could': 0.32; 'docs': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'changed': 0.34; 'doing': 0.35; 'add': 0.36; 'but': 0.36; '12,': 0.36; 'skip:{ 10': 0.36; "didn't": 0.36; 'should': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'free': 0.61; 'back': 0.62; 'necessarily': 0.63; 'within': 0.64; 'header:Reply- To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'transfer': 0.76; 'subject:this': 0.84; 'everything.': 0.84; 'locals': 0.84; 'reply- to:addr:python.org': 0.84; 'net.': 0.91; 'angel': 0.93
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=Zlz_JLF8fdcA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=xC9kh8UGFvsA:10 a=7m0NOpa_r8SQpW7qWFoA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Tue, 12 Feb 2013 15:15:14 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Python3 exec locals - this must be a FAQ
References <anuoc6F8kvaU1@mid.dfncis.de> <511A434D.4030309@davea.name>
In-Reply-To <511A434D.4030309@davea.name>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
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.1702.1360682099.2939.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360682099 news.xs4all.nl 6986 [2001:888:2000:d::a6]:50239
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38756

Show key headers only | View raw


On 2013-02-12 13:27, Dave Angel wrote:
> On 02/12/2013 06:46 AM, Helmut Jarausch wrote:
>> Hi,
>>
>> I've tried but didn't find an answer on the net.
>>
>> 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.
>>
>> E.g.  I have a lot of local names.
>>
>> Doing
>>
>> _locals= locals()
>
> This doesn't copy everything.  But perhaps you know that and you're just
> testing us.
>
>> 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?
>
> locals()["Var1"] = _locals["Var1"]  will set the same Var1 local.
>
> So you might write a loop on _locals.
>
> But beware if someone has deleted one of the "variables" it may not do
> what you'd like.  You cannot necessarily add back a local with the above
> syntax.
>
The docs for locals() warns """The contents of this dictionary should
not be modified; changes may not affect the values of local and free
variables used by the interpreter."""

That's because local variables in a function are implemented (at least
in CPython) using 'slots' for efficiency reasons.

 >>> def example():
     a = 1
     print("a is {}, locals() is {}".format(a, locals()))
     print("Changing a to 2 directly")
     a = 2
     print("a is {}, locals() is {}".format(a, locals()))
     print("Changing a to 3 via locals()")
     locals()["a"] = 3
     print("a is {}, locals() is {}".format(a, locals()))
     locals()["b"] = 0
     print("locals() is {}".format(locals()))
     print("b is {}".format(b))

 >>> example()
a is 1, locals() is {'a': 1}
Changing a to 2 directly
a is 2, locals() is {'a': 2}
Changing a to 3 via locals()
a is 2, locals() is {'a': 2}
locals() is {'b': 0, 'a': 2}
Traceback (most recent call last):
   File "<pyshell#39>", line 1, in <module>
     example()
   File "<pyshell#38>", line 12, in example
     print("b is {}".format(b))
NameError: global name 'b' is not defined

Note how attempting to change variable "a" in locals() is ignored, and
how adding variable "b" to locals() doesn't actually make it a local
variable (it raises a NameError).

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python3 exec locals - this must be a FAQ Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-02-12 11:46 +0000
  Re: Python3 exec locals - this must be a FAQ Dave Angel <davea@davea.name> - 2013-02-12 08:27 -0500
    Re: Python3 exec locals - this must be a FAQ Helmut Jarausch <jarausch@skynet.be> - 2013-02-12 14:29 +0000
      Re: Python3 exec locals - this must be a FAQ Dave Angel <davea@davea.name> - 2013-02-12 10:40 -0500
        Re: Python3 exec locals - this must be a FAQ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-13 11:05 +1100
  Re: Python3 exec locals - this must be a FAQ MRAB <python@mrabarnett.plus.com> - 2013-02-12 15:15 +0000
  Re: Python3 exec locals - this must be a FAQ Terry Reedy <tjreedy@udel.edu> - 2013-02-12 10:50 -0500
  Re: Python3 exec locals - this must be a FAQ Dave Angel <davea@davea.name> - 2013-02-12 11:40 -0500

csiph-web