Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Fri Nov 4 04:07:38 EST 2005


Op 2005-11-03, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> On Thu, 03 Nov 2005 13:35:35 +0000, Antoon Pardon wrote:
>
>> Suppose I have code like this:
>> 
>>   for i in xrange(1,11):
>>     b.a = b.a + i
>> 
>> Now the b.a on the right hand side refers to A.a the first time through
>> the loop but not the next times. I don't think it is sane that which
>> object is refered to depends on how many times you already went through
>> the loop.
>
> Well, then you must think this code is *completely* insane too:
>
> py> x = 0
> py> for i in range(1, 5):
> ...     x += i
> ...     print id(x)
> ... 
> 140838200
> 140840184
> 140843160
> 140847128
>
> Look at that: the object which is referred to depends on how many times
> you've already been through the loop. How nuts is that?

It is each time the 'x' from the same name space. In the code above
the 'a' is not each time from the same namespace.

I also think you new very well what I meant.

-- 
Antoon Pardon



More information about the Python-list mailing list