Class Variable Access and Assignment

Steven D'Aprano steve at
Sun Nov 6 02:21:39 CET 2005

On Sat, 05 Nov 2005 16:27:00 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at> writes:
>> But do you want x += y to work for immutable objects as well? Then
>> __iadd__ cannot be a statement, because x can't be modified in place.
> It never occurred to me that immutable objects could implement __iadd__.
> If they can, I'm puzzled as to why.  


The classic += idiom comes from C, where you typically use it on ints and

In C, ints aren't objects, they are just bytes, so you can modify them
in place. I'm surprised that it never occurred to you that people might
want to do something like x = 1; x += 1 in Python, especially as the
lack of such a feature (as I recall) was one of the biggest complaints
from C programmers crossing over to Python.

Personally, I'm not fussed about +=. Now that it is in the language, I'll
use it, but I never missed it when it wasn't in the language.

>> While I am enjoying the hoops people are jumping through to modify the
>> language so that b.a += 2 assigns b.a in the same scope as it was
>> accessed, I'm still rather perplexed as to why you would want that
>> behaviour.
> Weren't you the one saying += acting differently for mutables and
> immutables was a wart?  

Nope, not me.

> If it's such a wart, why are do you find it so
> important to be able to rely on the more bizarre consequences of the
> wartiness?  Warts should be (if not fixed) avoided, not relied on.

The consequences of instance.attribute += 1 may be unexpected for those
who haven't thought it through, or read the documentation, but they aren't
bizarre. Whether that makes it a feature or a wart depends on whether you
think non-method attributes should be inherited or not. I think they
should be.

I can respect the position of somebody who says that only methods
should be inherited -- somebody, I think it was you, suggested that there
is at least one existing OO language that doesn't allow inheritance for
attributes, but never responded to my asking what language it was.
Personally, I would not like an OO language that didn't inherit
attributes, but at least that is consistent. (At least, if you don't
consider methods to be a particular sort of attribute.)

But I can't understand the position of folks who want inheritance but
don't want the behaviour that Python currently exhibits.
instance.attribute sometimes reading from the class attribute is a feature
of inheritance; instance.attribute always writing to the instance is a
feature of OOP; instance.attribute sometimes writing to the instance and
sometimes writing to the class would be, in my opinion, not just a wart
but a full-blown misfeature.

I ask and I ask and I ask for some use of this proposed behaviour, and
nobody is either willing or able to tell me where how or why it would be
useful. What should I conclude from this?


More information about the Python-list mailing list