Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python3 exec locals - this must be a FAQ

Reply
Thread Tools

Re: Python3 exec locals - this must be a FAQ

 
 
Terry Reedy
Guest
Posts: n/a
 
      02-12-2013
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Python3 exec locals - this must be a FAQ Dave Angel Python 2 02-13-2013 12:05 AM
Re: Python3 exec locals - this must be a FAQ Dave Angel Python 0 02-12-2013 04:40 PM
Re: Python3 exec locals - this must be a FAQ MRAB Python 0 02-12-2013 03:15 PM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments