[Tutor] silly question

Orri Ganel singingxduck at gmail.com
Thu Dec 23 01:58:55 CET 2004


On Wed, 22 Dec 2004 17:52:53 -0700, Jason Child <jasonchild at cnsp.com> wrote:
> Ok, I guess my question (now) is:
> 
> how do I change global variables within a function:
> 
> ##################################
> VAR = "TEST"
> 
> def m():
>     VAR="no test"
> ##################################
> 
> when I do this (from the interactive editor):
> 
> ##################################
>  >>>print VAR
> TEST
>  >>>m()
>  >>>
>  >>>print VAR
> TEST
>  >>>
> ##################################
> 
> any advice?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

>>> VAR = "TEST"
>>> def m():
	global VAR              ## <- this is what you were missing . . .
basically, this means
	VAR = "no test"       ## that, instead of assigning to a new variable
VAR in the local
                                      ## namespace of m(), you are
assigning to the variable VAR that
                                      ## was defined on the global level

	
>>> print VAR
TEST
>>> m()
>>> print VAR
no test

HTH,
Orri

-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.


More information about the Tutor mailing list