global variables

Andreas Kostyrka andreas at kostyrka.priv.at
Sat Jul 27 16:11:22 EDT 2002


Am Mon, 2002-07-22 um 22.47 schrieb Michael Chermside:
> Python has global variables... any variable declared at the "top level" 
> (ie, not inside a function or class) is a global variable. The only 
> trick that you need to know is this: global variables are tracked on a 
> per-module basis. This means that if your code is all in one module, 
> then you can go ahead and just use globals.
Well, it's not that trivial. If you want to assign to global (module
level) variables inside some function/method, you need to declare this
by using global:

mylist=[]

def clearList():
	global mylist
	mylist=[]

def addToList(item):
	mylist.append(item)		# this modifies the list
					# but does not assign anything 
					# new to mylist.

Andreas





More information about the Python-list mailing list