Message73799
Right. Nested scopes only work for statically compiled code; inside an
'exec' block, non-local variables are global.
In your case, I suggest to specify the context in which the code has to
execute:
x = 2
def f():
g = globals().copy()
exec "x = 1\ndef b(): return x" in g
print eval("b()", g)
This prints 1, as you want it, and can still access names defined
outside the f() function. And this gives you more control on the symbols
available to the executed code.
Python does something very similar when it loads a module.
I don't think that this behaviour can ever change. Tentatively closing
as "won't fix". |
|
Date |
User |
Action |
Args |
2008-09-25 16:10:52 | amaury.forgeotdarc | set | recipients:
+ amaury.forgeotdarc, sandberg |
2008-09-25 16:10:52 | amaury.forgeotdarc | set | messageid: <1222359052.66.0.732773364867.issue3963@psf.upfronthosting.co.za> |
2008-09-25 16:10:48 | amaury.forgeotdarc | link | issue3963 messages |
2008-09-25 16:10:47 | amaury.forgeotdarc | create | |
|