Newbie question.

andres at corrada.com andres at corrada.com
Tue Jun 6 08:44:21 EDT 2000


On Tue, Jun 06, 2000 at 12:27:37PM +0100, Pablo Prieto wrote:
> Hi all of you there!
> 
> #This is my question
> 
> AYS = ""
> 
> def setEnviroment():
>   AYS = "NUMBER 9"
> 
> def getEnviroment():
>   return AYS
> 
> #This is the end...
> 
> The function getEnviroment always return "". I guess AYS is declared
> twice, One is module-scope and the other is local to setEnviroment, so
> I've got two differents variables.
> 

You need to use the "global" statement on AYS to prevent it from being
shadowed by the local namespace in the functions. So something like the
following will work for you:

>>> a = '' 
>>> def setEnvironment(): 
...     global a 
...     a = 'number9'
... 
>>> def getEnvironment(): 
...     global a 
...     return a 
... 
>>> getEnvironment() 
'' 
>>> setEnvironment() 
>>> getEnvironment() 
'number9' 

------------------------------------------------------
Andres Corrada-Emmanuel   Email: andres at corrada.com
------------------------------------------------------




More information about the Python-list mailing list