How to pass a global variable to a module?

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Sep 29 19:39:39 CEST 2009


Mars creature wrote:
> Dear Python users,
>   I just start to use python and love this language. I met this
> problem when I try to save my functions in a separate file.
> The question is how I can pass a global variable to a function which
> is saved in another file. If I save the function I defined in the same
> file with the main program, there is no problem after I declare the
> global variable. But problem comes out when I save all the function is
> a separate file. Help is very much appreciated! Thanks!
> Jinbo
>   
Do not use global variable, that's evil !

in file1.py:

myVar = 'foo'


in file2.py:
import file1

print file1.myVar
 >>> 'foo'

file1.myVar = 'bar'
print file1.myVar
 >>> 'bar'

Keep your variables ordered on their shelf.

JM



More information about the Python-list mailing list