P
Peter J. Bismuti
I'm having trouble understanding how namespaces work in modules. I want to
execute a module within the interpreter and then have values that are
calculated persist so that other modules that get executed can retrieve them.
For example, consider the two simple modules below. The first method fails
and I'm not sure exactly why. (Note: assume one instance of an interpreter.
In my case a 3rd party software tool that starts an interpreter when it
launches).
Two alternate ways of running it:
1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver using
the conditional statement if (__name__=="__main__"):
test.py
run2.py
or,
2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.
run.py
_________test.py__________________
import sys,os
A = 0
def getA():
global A
return A
def run():
global A
A = 10
if (__name__=="__main__"):
run()
_________run.py__________________
import test
test.run()
print "A = " + str(test.getA())
_________run2.py__________________
import test
print "A = " + str(test.getA())
--
Peter Bismuti
Boeing Information Technology
Renton, WA
(425) 234-0873 W
(425) 442-7775 C
execute a module within the interpreter and then have values that are
calculated persist so that other modules that get executed can retrieve them.
For example, consider the two simple modules below. The first method fails
and I'm not sure exactly why. (Note: assume one instance of an interpreter.
In my case a 3rd party software tool that starts an interpreter when it
launches).
Two alternate ways of running it:
1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver using
the conditional statement if (__name__=="__main__"):
test.py
run2.py
or,
2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver.
run.py
_________test.py__________________
import sys,os
A = 0
def getA():
global A
return A
def run():
global A
A = 10
if (__name__=="__main__"):
run()
_________run.py__________________
import test
test.run()
print "A = " + str(test.getA())
_________run2.py__________________
import test
print "A = " + str(test.getA())
--
Peter Bismuti
Boeing Information Technology
Renton, WA
(425) 234-0873 W
(425) 442-7775 C