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

Guido van Rossum guido@digicool.com
Wed, 28 Feb 2001 20:08:32 -0500


> Hi. Is the following true?
> 
> PEP227 states:
> """
> If the global statement occurs within a block, all uses of the
> name specified in the statement refer to the binding of that name
> in the top-level namespace.
> """
> 
> but this is a bit ambiguous, because the global decl (I imagine for
> backw-compatibility)
> does not affect the code blocks of nested (func) definitions. So
> 
> x=7
> def f():
>   global x
>   def g():
>     exec "x=3"
>     return x
>   print g()
> 
> f()
> 
> prints 3, not 7.

Unclear whether this should change.  The old rule can also be read as
"you have to repeat 'global' for a variable in each scope where you
intend to assign to it".

> PS: this improve backw-compatibility but the PEP is ambiguous or
> block concept does not imply nested definitions(?). This affects
> only special cases but it is quite strange in presence of nested
> scopes, having decl that do not extend to inner scopes.

--Guido van Rossum (home page: http://www.python.org/~guido/)