PDA

View Full Version : a note on Python's "global"


WilliamM
05-31-2008, 12:46 PM
In traditional (compiled procedural) languages, you would usually declare a global variable at the top-level, and then use it throughout functions/procedures as you would a local variable without declaring it again. Within each function there is no indication that the variable is global because those are the scoping rules of the language: if you define a variable outside of a function, it's global.

Python is different. A variable declared as global (even at the top-level) can be "read" without difficulty, but you can't "write" to it without first telling Python that you wish to do so (by using the global statement again within the function). Forgetting to do so results in the creation of a similarly named local variable, with no relation to the intended global variable. Thus, putting "global variableName" within a function can be though of as telling Python it's ok to change the variable.

global x #redundant 'top-level' declaration
x = 1

def read_x():
return x #references global variable x

def wont_modify_global_x():
x = 2 #creates a local variable called x

def will_modify_global_x():
global x #makes global variable x locally accessible for modifying
x = 2 #modifies global variable x

kbanks
06-02-2008, 08:37 AM
This is also covered in the SNAP Reference Manual, in the SNAPpy Scripting Hints section (look for "Beware of Accidental Local Variables", right after "Beware of Case SensitiViTy").

njones
09-17-2009, 02:37 PM
Is it necessary to define the global variable at the top of the script? If you declare and use a global in a few functions will the first one to "use it" actually create it?

I'm creating a "reset" function that clears all globals to a known state at startup and also under certain runtime conditions. It seems redundant to have the variables initialized at the top of the script, then have some duplicate code for re-initialization. Script space is somewhat limited and I'm trying to minimize my footprint.

For example:

x = 1

def resetStuff():
global x, y
y = 0
x = 0

def doSomething():
global x, y
x += 1
y += 1
if x == 3:
resetStuff()

def getX():
return x

def getY():
return y

def _startup():
resetStuff()

snappyGen.setHook(SnapConstants.HOOK_STARTUP, _startup)


This works (version 2.1), even the getY function returns the "global" Y even though it is not declared at the top. Both X and Y behave the same way so it seems that the X = 0 at the top is not necessary. Of course I would expect Y to be "nothing" until resetStuff() was called the first time.

I'm concerned whether it is an "approved" method to skip declaring globals and whether scriptios writtent his way will continue to work with future versions of SNAPpy.

kbanks
09-17-2009, 06:06 PM
I'm concerned whether it is an "approved" method to skip declaring globals and whether scriptios writtent his way will continue to work with future versions of SNAPpy.

Script writers choice... just be sure to initialize them one place or the other (either at the top of the file, or in your startup function).