On Sat, Sep 12, 2015 at 1:27 AM, Ian Kelly 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 = [1]
... exec("print(x); x[0] = 2; print(x); x = [3]; print(x)")
... print(x)

It's kinda like how globals can shadow builtins, I think. Maybe.
Except that you can't del the name inside exec to unshadow and go back
to the outer version of it.


Search Discussions

Discussion Posts


Follow ups

Related Discussions



site design / logo © 2018 Grokbase