Newbie question.

Ken Seehof 12klat at sightreader.com
Tue Jun 6 09:35:54 EDT 2000


You're right.  By default assignments inside functions create local
variables within the function, even if there happens to be a variable by
the same name in the module scope.

Specify 'global' to make your function assign to the module scope.

Example:

>>> AYS = "hey"
>>> def setEnviroment():
...  global AYS
...  AYS = "NUMBER 9"
...
>>> def getEnviroment():
...    return AYS
...
>>> getEnviroment()
'hey'
>>> setEnviroment()
>>> getEnviroment()
'NUMBER 9'

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.
>
> How can I make the variable AYS global to setEnviroment, so I'd be able
> to return "NUMBER 9" in getEnviroment?. (Don't say return "NUMBER9".
> I'll get annoyed :))
>
> Well, Have a nice summer all of you (or Winter if you are in the other
> side of the floor).
>
> Pablo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/ee160dca/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 12klat.vcf
Type: text/x-vcard
Size: 343 bytes
Desc: Card for Ken Seehof
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/ee160dca/attachment.vcf>


More information about the Python-list mailing list