By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

N00b question on Py modules

P: n/a
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

May 7 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In <11**********************@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
May 7 '07 #2

P: n/a
On Mon, May 07, 2007 at 12:00:38AM -0700, lo************@gmail.com wrote:
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

May 7 '07 #3

P: n/a
lo************@gmail.com wrote:
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

Thanks

May 7 '07 #4

P: n/a
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.
May 7 '07 #5

P: n/a
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

May 7 '07 #6

P: n/a
<lo************@gmail.comwrote:
>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.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
May 9 '07 #7

P: n/a
Ant
On May 9, 2:47 pm, Sion Arrowsmith <s...@chiark.greenend.org.uk>
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!
May 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.