Tracing variable scope (local vs. global)

M

Manuel Graune

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
 
L

Lie Ryan

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).
 
D

Diez B. Roggisch

Manuel said:
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
 
T

Terry Reedy

Manuel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top