473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

N00b question on Py modules

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
7 1090
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
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
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
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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I'd like to overwrite just one line of a binary file, based on a position set by seek(). Is there no way to do this? As far as I can tell I need to read the whole file, change the line, and write...
3
by: Anupam Kapoor | last post by:
hi all, a python n00b, so please bear with me. i have a simple question: i generally name python sources as a-simple-python-example.py. when i try to import a module named as above, i...
1
by: newgenre | last post by:
I am using a pre-built package of code for my site, which is called EasyDisc. All it does is it creates an interactive forum on your site, like any forum you see anywhere. I am having a problem...
1
by: Ambush | last post by:
I'm pretty new to ASP.NET programming and web programming in general, but I've been a client/server programmer for several years now (which is definately making things more difficult for me). I've...
7
by: Alan | last post by:
When you use a function like I see posted here often, like: Public Function Whatever() stuff... End Function What do you actually do with that to make it run? I assumed you put it in a...
2
by: ducky | last post by:
Hi all, The only programming experience i have under my belt so far is VB. I'm just starting out on C++ and wonder if anybody suggests and good (free) starting points for me to get going. I'm...
4
by: onefry | last post by:
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming here it is #include <iostream> #include <cstdlib> using namespace std;
6
by: Charles | last post by:
I am learning from the Accelerated C++ book. The following example doesn't work and I don't know why: #include <iostream> #include <string> int main () { const std::string exclam = "!"; const...
4
by: sublimanized | last post by:
Hello all ... here is my problem. I just got the book "Teach Yourself C ++ in 21 Days" Setup: Fedora Core 6 - i386 GCC 4.1.1 Again, I am a complete newcomer to C++, and this is one of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.