N00b question on Py modules

L

lokesh.jagasia

Hi. Sorry to sound like a noob but that's what I am when it comes to
Python. I just wrote the below module and it behaves funny.

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.

I've been through the modules section of Python docs and a few ebooks
as well, all suggest that it shouldn't be working this way.

Please help out ppl.

Thanks
 
M

Marc 'BlackJack' Rintsch

lokesh.jagasia said:
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
 
C

Christoph Haas

Hi. Sorry to sound like a noob but that's what I am when it comes to
Python. I just wrote the below module and it behaves funny.

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.

_exitcode is a global variable in your program. In functions (def) you
can read global variables. But if you change or reassign them the change
will be only local to the function. If you want to change the global
variable your def needs to be:

def setExitCode():
global _exitcode
_exitcode = 1

Kindly
Christoph
 
G

Gary Herron

Hi. Sorry to sound like a noob but that's what I am when it comes to
Python. I just wrote the below module and it behaves funny.

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.

I've been through the modules section of Python docs and a few ebooks
as well, all suggest that it shouldn't be working this way.

Please help out ppl.

It's a scoping problem. The line

_exitcode = 0

creates a (module level) global object.

But in

def setExitCode():
_exitcode = 1

you are running into Python's default presumption that variables assigned to in a function are *local* to that function. And like all local variables, they can be set and used within the function, but are independent of objects outside the function.

If you want to assign to a global object from within a function, then you must explicitly say so:

def setExitCode():
global _exitcode
_exitcode = 1

See: http://docs.python.org/ref/global.html

Gary Herron
 
S

Steven D'Aprano

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
 
L

lokesh.jagasia

Thanks a lot for the responses ppl. Python's treatment of global
variables was an eye-opener. I have coded in Java & C/C++ in the past
and there the behaviour is diametrically opposite.

Cheers
 
S

Sion Arrowsmith

Thanks a lot for the responses ppl. Python's treatment of global
variables was an eye-opener. I have coded in Java & C/C++ in the past
and there the behaviour is diametrically opposite.

How so? Python style gurus discourage use of global variables. So
does all the C++ (and to a lesser extent C) advice I've ever
encountered. And Java outright forbids the concept. It's one area
where there seems to be universal agreement.
 
A

Ant

On May 9, 2:47 pm, Sion Arrowsmith <[email protected]>
wrote:
....
How so? Python style gurus discourage use of global variables. So
does all the C++ (and to a lesser extent C) advice I've ever
encountered. And Java outright forbids the concept.

Class variables (public static), are the equivalent of global
variables in Java, and can be an equal pain. Singleton objects can
cause similar problems, since they are essentially global objects, and
changing the values of any of their members can cause wierd behaviour
in otherwise unrelated parts of the code. So Java isn't by any means
immune to the global variable problem, it just has different names for
them!
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top