Tracing variable scope (local vs. global)

Diez B. Roggisch deets at nospam.web.de
Tue Dec 1 17:36:39 EST 2009


Manuel Graune schrieb:
> Hello,
> 
> consider the following piece of code:
> 
> a=1
> b=2
> 
> def foo(c):
>     b=3
>     return a + b + c
> 
> In this case, when calling "foo", "a" will take the global value,
> "b" will take the local value and "c" will take the value assigned
> when calling the function.
> 
> Since I consider this behaviour a possible source of bugs due to
> personal sloppiness (e. g. forgetting to put "a=4" inside the
> function-body):
> 
> Is there any way to automatically check that all variables in a
> function are either local or passed in as arguments?

No. And as long as you don't have the habit of using global variables to 
a non-reasonable extent, and follow some simple conventions such as 
spelling them in all uppercase and with more meaningful names such as 
the ones above - it's not an actual problem.

Diez



More information about the Python-list mailing list