P
PGMoscatt
Does Python allow a 'global' variable that can be used between different
modules used within an app ?
Pete
modules used within an app ?
Pete
PGMoscatt said:Does Python allow a 'global' variable that can be used between different
modules used within an app ?
Pete
Roy said:You could explicitly import __main__ and use that as a global namespace
(see example below). Or (perhaps a better idea), you could have a module
dedicated to global storage, and import that in all your other modules
instead of importing __main__.
---------------------------------
Roy-Smiths-Computerlay$ cat global.py
#!/usr/bin/env python
import a
import b
a.set()
print "x = %s" % x
print "b.get() = %s" % b.get()
---------------------------------
Roy-Smiths-Computerlay$ cat a.py
import __main__
def set():
__main__.x = "hello"
---------------------------------
Roy-Smiths-Computerlay$ cat b.py
import __main__
def get():
return __main__.x
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.