[Python-Dev] nested scopes. global: have I got it right?

Jeremy Hylton jeremy@alum.mit.edu
Thu, 1 Mar 2001 16:05:51 -0500 (EST)


> x=7
> def f():
>   global x
>   def g():
>     exec "x=3"
>     return x
>   print g()
> 
> f()
> 
> prints 3, not 7.

I've been meaning to reply to your original post on this subject,
which actually addresses two different issues -- global and exec.  The
example above will fail with a SyntaxError in the nested_scopes
future, because of exec in the presence of a free variable.  The error
message is bad, because it says that exec is illegal in g because g
contains nested scopes.  I may not get to fix that before the beta.

The reasoning about the error here is, as usual with exec, that name
binding is a static or compile-time property of the program text.  The
use of hyper-dynamic features like import * and exec are not allowed
when they may interfere with static resolution of names.

Buy that?

Jeremy