D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] Global variables in Python?

 

Hi all,

Having a bit of a problem with getting my head around how Python handles global variables.

Here's an example snippet of code:
----Python Code----
#!/usr/bin/env python
global SOMEVAR
SOMEVAR = "Hello"

def FNsomefunction():
  SOMEVAR = "Goodbye"

print SOMEVAR
FNsomefunction()
print SOMEVAR
----End of Python Code----

When this is run, the output is Hello twice.

Now, to my brain since SOMEVAR is a global variable, the contents should be accessible from any part of the code, and should be changeable from any part of the code too.

Am I thinking about, or implementing this in completely the wrong way?

Cheers.
Grant. :)
-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq