Python handles globals badly.
ian.g.kelly at gmail.com
Fri Sep 11 17:49:47 CEST 2015
On Fri, Sep 11, 2015 at 9:44 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Sep 12, 2015 at 1:27 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> The exec still happily runs; it's just using its own private locals namespace.
>> Tangent: does the help for exec need to be updated? It currently reads:
>> The globals and locals are dictionaries, defaulting to the current
>> globals and locals. If only globals is given, locals defaults to it.
>> Which would seem to indicate that if called from within a function
>> with no globals or locals, the locals from the function would be used.
> And that's the thing... I think. It's using locals(), which starts out
> as a copy of the function's locals (in this example, empty), but
> without assignment affecting anything. Which is more than a little
>>>> def f():
> ... x = 
> ... exec("print(x); x = 2; print(x); x = ; print(x)")
> ... print(x)
Ah, that makes sense. It's writing into the dict that is created and
returned by locals(), but not actually updating the frame locals which
are the source of truth.
More information about the Python-list