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


Groups > comp.lang.python > #38748

Re: Python3 exec locals - this must be a FAQ

Date 2013-02-12 08:27 -0500
From Dave Angel <davea@davea.name>
Subject Re: Python3 exec locals - this must be a FAQ
References <anuoc6F8kvaU1@mid.dfncis.de>
Newsgroups comp.lang.python
Message-ID <mailman.1699.1360675679.2939.python-list@python.org> (permalink)

Show all headers | View raw


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.


-- 
DaveA

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