![](https://secure.gravatar.com/avatar/0b854fe5258eb97b09ae8f914ab9d598.jpg?s=120&d=mm&r=g)
"SP" == Samuele Pedroni <pedroni@inf.ethz.ch> writes:
SP> # top-level SP> def g(): SP> exec "x=3" SP> return x
At the top-level, there is no closure created by the enclosing scope is not a function scope. I believe that's the right thing to do, except that the exec "x=3" also assign to the global.
I'm not sure if there is a strong justification for allowing this form, except that it is the version of exec that is most likely to occur in legacy code.
Unfortunately this used to work, using a gross hack: when an exec (or import *) was present inside a function, the namespace semantics *for that function* was changed to the pre-0.9.1 semantics, where all names are looked up *at run time* first in the locals then in the globals and then in the builtins. I don't know how common this is -- it's pretty fragile. If there's a great clamor, we can put this behavior back after b1 is released. --Guido van Rossum (home page: http://www.python.org/~guido/)