scope of variable

John Nagle nagle at animats.com
Sat Aug 21 02:40:53 EDT 2010


On 8/20/2010 12:56 PM, M B wrote:
> fre 2010-08-20 klockan 13:19 -0600 skrev Burton Samograd:
>> M B<znarus at telia.com>  writes:
>>
>>> Hi,
>>>>>> dept=0
>>>>>> def mud():
>>> 	print dept
>>>
>>> 	
>>>>>> mud()
>>> 0
>>>>>> def mud():
>>> 	dept+=1
>>> 	print dept
>>
>> You should add a global statement or else python thinks a variable used
>> is a local:
>>
>>>>> def mud():
>>      global dept
>>   	dept+=1
>>   	print dept
>>
>> --
>> Burton Samograd
>>
> Ok.  Thanks for the answers. :)  I was a bit puzzled of
> the fact that I could read but not assign to a global variable.

    If you assign before a read, you create a local variable.

    If you read only, you get a global access.

    If you do a read before an assignment, you get an error message.

				John Nagle




More information about the Python-list mailing list