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

 
 
MRAB
Guest
Posts: n/a
 
      02-12-2013
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).

 
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 Terry Reedy Python 0 02-12-2013 03:50 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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57