[Tutor] Global variables

Lloyd Kvam pythontutor@venix.com
Thu, 20 Dec 2001 15:59:01 -0500

Any variable that is the target of an assignment statement is automatical=
marked as a local variable.  You need to use the global statement first
if you want to assign to a global variable.

totalerrors =3D 0
def CheckErrors(statuscode):
	global totalerrors
	if statuscode < 0:

You will need to repeat the global statement within each scope - for
each function def in your example.

Lee-Shanok, Bruce wrote:

> Hello all,
> 	I'm trying to declare a global variable to keep count of a value
> without constantly passing it around. The problem I'm running into is t=
> it's not being recognized in any of my functions. The basic format is:
> totalerrors =3D 0
> def CheckErrors(statuscode):
> 	if statuscode < 0:
> 		totalerrors =3D totalerrors + 1
> 		print "Error with code",
> 		print statuscode
> def RunTest():
> 	status =3D Func1()
> 	CheckErrors(status)
> 	status =3D Func2()
> 	CheckErrors(status)
> 	status =3D Func3()
> 	CheckErrors(status)
> 	status =3D Func4()
> 	CheckErrors(status)
> RunTest()
> .. is there any way for me to get totalerrors recognized? How does Pyth=
> determine where the scope of the variable ends?
> Bruce Lee-Shanok
> Access Manager
> (613) 738-1338 ext 5764
> Cognos, Ottawa=20
> [Riverside, 4th Floor, E9]
> Ce message peut contenir des informations prot=E9g=E9es et/ou confident=
> Si vous avez re=E7u ce mail par erreur ou si vous n'en =EAtes pas le
> destinataire, il vous est interdit d'utiliser, de copier, de diffuser o=
u de
> distribuer les pi=E8ces qui y sont jointes. Supprimez-le et notifiez
> imm=E9diatement par e-mail la personne qui vous l'a envoy=E9e. Merci d'=
> This message may contain privileged and/or confidential information.  I=
f you
> have received this e-mail in error or are not the intended recipient, y=
> may not use, copy, disseminate, or distribute it; do not open any
> attachments, delete it immediately from your system and notify the send=
er by
> e-mail promptly that you have done so.  Thank You.
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358