[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=
ly
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,
>=20
> 	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=
hat
> it's not being recognized in any of my functions. The basic format is:
>=20
> totalerrors =3D 0
> def CheckErrors(statuscode):
> 	if statuscode < 0:
> 		totalerrors =3D totalerrors + 1
> 		print "Error with code",
> 		print statuscode
>=20
> def RunTest():
> 	status =3D Func1()
> 	CheckErrors(status)
> 	status =3D Func2()
> 	CheckErrors(status)
> 	status =3D Func3()
> 	CheckErrors(status)
> 	status =3D Func4()
> 	CheckErrors(status)
>=20
> RunTest()
>=20
> .. is there any way for me to get totalerrors recognized? How does Pyth=
on
> determine where the scope of the variable ends?
>=20
> Bruce Lee-Shanok
> Access Manager
> (613) 738-1338 ext 5764
> Cognos, Ottawa=20
> [Riverside, 4th Floor, E9]
>=20
> Ce message peut contenir des informations prot=E9g=E9es et/ou confident=
ielles.
> 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'=
avance.
>=20
>=20
> 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=
ou
> 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.
>=20
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>=20
>=20


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

voice:=20
603-443-6155
fax:=20
801-459-9582