N00b question on Py modules

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon May 7 03:22:58 EDT 2007


On Mon, 07 May 2007 00:00:38 -0700, lokesh.jagasia wrote:

> I expected to see an output of 0 followed by 1. But it turns out that
> the _exitcode variable is not changed at all. It seems that
> setExitCode() might be editing a local copy of the _exitcode variable.

Yes, that's exactly what is happening.


> But then, how do I tell it to change the value of the module variable
> and not its local variable.

(1) Don't do that.

(2) If you think you really need to do it, you don't.

(3) If you REALLY need to do it, use the statement:

global <variable name>

in your function.

Over-use of global variables is one of the sins of programming. Some 
people might even say that using ANY global variables is a sin. I'm not 
that strict, but I encourage you to avoid global variables if you can.

See here for more details:

http://en.wikipedia.org/wiki/Global_variable




-- 
Steven.



More information about the Python-list mailing list