using 'global' across source files.
Bram Stolk
bram at sara.nl
Tue Jan 2 07:41:07 EST 2001
Hello,
Using the 'global' keyword, I can have my functions
access global variables, like:
# try.py
val=100
def change_val() :
global val
val = 2*val
change_val()
print val
However, this scheme falls apart if I split up this
code in two files, one containing the global, and
one containing the func.
# prog.py
from funcs import *
val = 100
change_val()
print val
# funcs.py
def change_val() :
global val
val = val * 2
When running I get an error:
bram at gazoo> python -i prog.py
Traceback (most recent call last):
File "prog.py", line 7, in ?
change_val()
File "funcs.py", line 5, in change_val
val = val * 2
NameError: There is no variable named 'val'
Why?
And how can I fix this?
I've tried stuff like __main__.val, but
that is not accepted by python.
thx,
Bram Stolk
--
------------------------------------------------------------------------------
Bram Stolk, VR Specialist.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: bram at sara.nl Phone +31-20-5923059 Fax +31-20-6683167
"I heard if you play the NT-4.0-CD backwards, you get a satanic message."
"Thats nothing, if you play it forward, it installs NT-4.0"
------------------------------------------------------------------------------
More information about the Python-list
mailing list