Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38770
| Date | 2013-02-12 11:40 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: Python3 exec locals - this must be a FAQ |
| References | <anuoc6F8kvaU1@mid.dfncis.de> <511A434D.4030309@davea.name> <kfdobk$r6s$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1713.1360687277.2939.python-list@python.org> (permalink) |
On 02/12/2013 10:50 AM, Terry Reedy wrote:
> On 2/12/2013 8:27 AM, Dave Angel wrote:
>> On 02/12/2013 06:46 AM, Helmut Jarausch wrote:
>
>>> <snip>
>>>
>>> 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.
The OP presumably wanted to restore the original values of the original
variables. The above "assignment" won't help a bit with that.
>
> 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"].
>
My claim was based on the assumption that the earlier assignment had
been fixed by some kind of copy. If not, there's nothing to restore.
I also retracted my use of that trick anyway, since being corrected by
MRAB. I only tested it in top-level code, not inside a function.
--
DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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