Author amaury.forgeotdarc
Recipients amaury.forgeotdarc, sandberg
Date 2008-09-25.16:10:47
SpamBayes Score 8.60662e-12
Marked as misclassified No
Message-id <1222359052.66.0.732773364867.issue3963@psf.upfronthosting.co.za>
In-reply-to
Content
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".
History
Date User Action Args
2008-09-25 16:10:52amaury.forgeotdarcsetrecipients: + amaury.forgeotdarc, sandberg
2008-09-25 16:10:52amaury.forgeotdarcsetmessageid: <1222359052.66.0.732773364867.issue3963@psf.upfronthosting.co.za>
2008-09-25 16:10:48amaury.forgeotdarclinkissue3963 messages
2008-09-25 16:10:47amaury.forgeotdarccreate