N00b question on Py modules
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Mon May 7 03:11:56 EDT 2007
In <1178521238.333095.111370 at h2g2000hsg.googlegroups.com>, lokesh.jagasia
wrote:
> My python module:
>
> _exitcode = 0
>
> def setExitCode():
> _exitcode = 1
>
> if __name__ == '__main__':
> print _exitcode
> setExitCode()
> print _exitcode
>
> Actual O/P:
> 0
> 0
>
> 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.
> But then, how do I tell it to change the value of the module variable
> and not its local variable.
Any name that gets bound to an object within a function is local to that
function unless you declare it as ``global``. But using lots of global
variables is considered bad style so you may think about rewriting
functions with ``global`` names to return the value(s) instead:
_exitcode = 0
def set_exitcode():
return 1
if __name__ == '__main__':
print _exitcode
_exitcode = set_exitcode()
print _exitcode
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list