Class Variable Access and Assignment

Antoon Pardon apardon at
Thu Nov 3 14:01:40 CET 2005

Op 2005-11-03, Steven D'Aprano schreef <steve at>:
> On Thu, 03 Nov 2005 11:55:06 +0000, Antoon Pardon wrote:
>> No matter wat the OO model is, I don't think the following code
>> exhibits sane behaviour:
>> class A:
>>   a = 1
>> b = A()
>> b.a += 2
>> print b.a
>> print A.a
>> Which results in
>> 3
>> 1
> Seems perfectly sane to me. 
> What would you expect to get if you wrote b.a = b.a + 2?

I would expect a result consistent with the fact that both times
b.a would refer to the same object.

> Why do you expect
> b.a += 2 to give a different result?

I didn't know I did.

> Since ints are immutable objects, you shouldn't expect the value of b.a
> to be modified in place, and so there is an assignment to b.a, not A.a.

You are now talking implementation details. I don't care about whatever
explanation you give in terms of implementation details. I don't think
it is sane that in a language multiple occurence of something like b.a
in the same line can refer to different objects

I think it even less sane, if the same occurce of b.a refers to two
different objects, like in b.a += 2

Antoon Pardon.

More information about the Python-list mailing list