In <[EMAIL PROTECTED]>, 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to