[Python-3000] The meaning of "global variable"

Georg Brandl g.brandl at gmx.net
Sun Nov 5 10:49:34 CET 2006


Ron Adam wrote:

> I presume you meant:
> 
>     def f():
>       x = 42
>       def g():
>         def h():
>           parent x
>           x = 88
> 
> 
> The x would be a new local x in function g and have no effect on the x in 
> function f.  Parent would do exactly what it means in this case, its a reference 
> to a name in only the parent scope.

This *is* confusing, even if the word "parent" is a slight hint on what's
really going on.

> Unless you were to do:
> 
>     def f():
>       x = 42
>       def g():
>         parent x
>         def h():
>           parent x
>           x = 88
> 
> Then all references to x would be the local x in function f.

"So why do I have to declare x in g, if I don't use it there?" This is really
ugly.

Georg



More information about the Python-3000 mailing list