Recently I had someone come ask me for a bit more information about working with global variables. For those new to python, this might be something helpful, so I figured I’d share. Personally, for ease of reference, I specify my global variables names in ALLUPPERCASE. This helps distinguish them since I use that naming standard nowhere else in my code.
In a python application you have multiple namespaces. Each namespace is intended to be completely isolated, so you can use the same name in multiple namespaces without conflict. The global namespace is the only one where this does not hold strictly true. If the below is not enough, a good and more in depth explanation is available here: A guide to Python Namespaces.
A global variable can be called from inside any namespace, but without a special call any changes stay inside that local namespace. If you state inside your function/class/whatever that you are using the global variable as a global, then you changes take place in the global name space.
Here is some sample code that show this in action:
GLOBAL = True
print "Inside 'changesGlobal' function GLOBAL is %s" % GLOBAL
print "Inside 'doesntChangeGlobal' function GLOBAL is %s" % GLOBAL
print "We start with GLOBAL as %s" % GLOBAL
print "After 'changesGlobal' GLOBAL is %s" % GLOBAL
print "After 'doesntChangeGlobal' GLOBAL is %s" % GLOBAL