Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Mon Nov 7 03:38:49 EST 2005


Op 2005-11-04, Magnus Lycka schreef <lycka at carmen.se>:
> Antoon Pardon wrote:
>  > I have looked and didn't find it in the language reference.
>> 
>> This is what I have found:
>> 
>> An augmented assignment expression like x += 1 can be rewritten
>> as x = x + 1 to achieve a similar, but not exactly equal effect.
>
> It's just a little further down. I'll post the quote once more (but
> this is the last time ;^):

I appreciate you quoting the documentation. But I would appreciate
a URL even more. It isn't necessary now any more but it would have
been usefull the first time you quoted this material.

> """For targets which are attribute references, the initial value is 
> retrieved with a getattr() and the result is assigned with a setattr(). 
> Notice that the two methods do not necessarily refer to the same 
> variable. When getattr() refers to a class variable, setattr() still 
> writes to an instance variable. For example:
>
> class A:
>      x = 3    # class variable
> a = A()
> a.x += 1     # writes a.x as 4 leaving A.x as 3"""
>
> I'd say it's documented...

Well then I guess they have documented awkward behaviour.

>> That doesn't change the fact that the current behaviour is
>> on occasions awkward or whatever you want to call it.
>
> I fear that this has to do with the way reality works. Perhaps
> it's due to Gödel's incompleteness theorems... :)
>
> Sure, Python has evolved and grown for about 15 years, and
> backward compatibility has always been an issue, but the
> management and development of Python is dynamic and fairly
> open-minded. If there had been an obvious way to change this
> in a way that solved more problems than it caused, I suspect
> that change would have happened already.

Fine I can live with that. 

-- 
Antoon Pardon



More information about the Python-list mailing list