Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tracing variable scope (local vs. global)

Reply
Thread Tools

Tracing variable scope (local vs. global)

 
 
Manuel Graune
Guest
Posts: n/a
 
      12-01-2009

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?

Regards,

Manuel Graune

--
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      12-01-2009
On 12/2/2009 9:02 AM, Manuel Graune wrote:
>
> 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?


If you really mean what you're saying, you won't be able to use builtin
functions.

Anyway... the answer to your problem is a strong naming convention and
to not store variables in the global namespace; put everything in a
class and use a main() function so your initialization code doesn't
clutter up the global namespace. As such, there shouldn't be a
lowercased name in the global namespace (class use CamelCase and
constants UPPERCASED); and all lowercased names are local (or builtins,
use pylint to check for the case of shadowing builtins).
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-01-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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-02-2009
Manuel Graune wrote:
> 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?


There are at least 3 code checking programs: pychecker, pylint,
pyflakes. Perhaps one of them has an option to check for non-builtin
globals. Would not be too difficult to add, I suspect.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments