Global variables for python applications

Chris Rebert clp2 at rebertia.com
Sun May 16 14:01:15 EDT 2010


On Sun, May 16, 2010 at 10:50 AM, AON LAZIO <aonlazio at gmail.com> wrote:
> Hi,
>    How can I set up global variables for the entire python applications?
> Like I can call and set this variables in any .py files.
>    Think of it as a global variable in a single .py file but this is for the
> entire application.

Thankfully, there is no such thing (can you say spaghetti code?). The
closest approximation, as I said in my previous reply, is to use the
namespace of a designated module for this purpose, and import that
module wherever you need to access/modify these "superglobal"
variables.

Example:
#g.py:
#this module exists to hold superglobal vars
global1 = "foo"
global2 = "bar"


#elsewhere.py:
#this is some other module in the same program
import mypackage.g as g

print "global #1 = ", g.global1
print "global #2 =", g.global2
g.global1 = "baz" # modify a superglobal
g.global3 = "qux" # create a new superglobal


Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list