python exec behaves inconsistent with respect to module imports

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Sep 5 21:11:08 EDT 2007


En Wed, 05 Sep 2007 11:45:11 -0300, carl.dhalluin at gmail.com  
<carl.dhalluin at gmail.com> escribi�:

> Thank you for the explanation
>
> It seems that python behaves different for variables created in the
> toplevel namespace, versus deeper namespaces.

Yes. Older Python versions had only two scopes: local and global. A name  
was either local or global. Now, there are nested scopes too.
If a name is bound inside a function (by example, an identifier at the  
left hand side of an assignment) it is local to that function (unless the  
global statement is used). The check is made at compile time.
If a name is bound at the module level, it is a global variable.
Names used in a function but defined on an enclosing function are free  
variables.

> CODE:
>
> def g():
>   a=2
>   print "a in LOC:",locals().has_key('a')
>   print "a in GLO:",globals().has_key('a')
>   def f():
>     print "a in LOC:",locals().has_key('a')
>     print "a in GLO:",globals().has_key('a')
>     print a
>   f()
> g()
>
> b=3
> print "b in LOC:",locals().has_key('b')
> print "b in GLO:",globals().has_key('b')
> def h():
>   print "b in LOC:",locals().has_key('b')
>   print "b in GLO:",globals().has_key('b')
>   print b
> h()
>
>
> DISCUSSION:
>
> locals() are passed through from a function to a nested function
> locals() are not passed if you go from top-level to a nested function.

No.
A function (more generally: a code block) can refer to local variables,  
free variables, or global variables.
Local variables are NOT stored in a dictionary, but on a fixed size array,  
and indexed by ordinal instead of name. The compiler knows exactly which  
variables are local at compile time, just looking at the code.
Free variables are accessed thru "cell" objects, which keep a reference to  
the enclosing scope. The compiler knows about free variables too (they are  
local to some enclosing scope). Try adding these lines at the end of g  
function above:

    f()
    a=-1
    f()

Global variables are those in the module where the function is defined  
(plus some built-in names) - they're searched by name at runtime.
Local variables are, uhm, local :) and not "passed" anywhere.

locals() is a dictionary built on-demand. It includes local variables  
*AND* free variables too.
In function f above, there are no local variables, and `a` is a free  
variable.
In function g, `a` and `f` are local variables.
In function h, `b` is a local variable.

> globals() are visible from all levels

Yes, unless an inner scope shadows the name. Putting b=5 at the first line  
on function h above hides the global `b`

> globals() are auto-populated at top level
Yes; global variables are those defined at the module level.
See the Language Reference <http://docs.python.org/ref/naming.html>

> So now get back to my exec code in the previous post. [...] To me this  
> seems a bug in python exec:

Now that you know a bit more about locals and globals, try to understand  
what exec does in your example.
Note that the exec statement has two optional arguments that are used as  
the global and local namespaces: <http://docs.python.org/ref/exec.html>

-- 
Gabriel Genellina




More information about the Python-list mailing list