Global variables in modules/functions

Aaron Deskins ndeskins at ecn.purdue.edu
Fri Nov 19 17:52:23 EST 2004


I'm trying to write program with a main.py and several functions 
distributed in auxiliary *.py files. I've looked through the archives on 
the 'global' keyword, but I'm still not sure how to get this right. I 
want to be able to have some variables that can be accessed/changed by 
many modules/functions.

Here's an example.

____________
main.py
____________
import change
n = 1
change.change_n()
print n

____________
change.py
____________
def change_n():
     global n
     n = 2


Running main.py gives this: 1. So n is not being changed when the 
function is called. I've tried adding a 'import from main *' line to 
change.py, but this only gives an error message.

Any ideas?

Thanks


-- 
Aaron Deskins
Graduate Student
Chemical Engineering
Purdue University



More information about the Python-list mailing list