inconsistency with += between different types ?

Andreas Leitgeb Andreas.Leitgeb at
Tue Aug 6 12:16:11 EDT 2002

Huaiyu Zhu <huaiyu at> wrote:
> That's what it should be.  Yet it is possible for whoever writes __iadd__ to
> define it the other way.  
Really ?

Inside __iadd__ I've got the "self"-parameter:
 If I assign directly to "self", then it won't have any effect outside 
   the method.
 If I assign to self.whatever, I mutate the object in place.
Is there something I've missed, or does it indeed boil down to 
 defining or not defining __ixxx__ ?

Also, what characterizes an object to be mutable ?
Is it the existence or not-existence of mutating methods, or
 is the more behind the scenes of strings,ints and tuples ?

> Consider this table:
>                 in place         assignment
>             +---------------+----------------+
> mutable     |       A       |        B       |
>             +---------------+----------------+
> immutable   |       C       |        D       |
>             +---------------+----------------+
How I understand it:  (and it looks quite reasonable that way)
(__iadd__ stands for all the __i...__-methods)
A: __iadd__ is defined
B: only __add__ is defined, not __iadd__
C: hmm, if __iadd__ is defined, then "per definition" the object
     is not immutable, thus not C :-)
D: neither __iadd__ nor any other mutating method is defined,
     but __add__ is.

> Consequently we are seeing frequent questions on this issue.  
If it actually works the way, I now think it does, then it's just
a lack of clear documentation. 

Newsflash: Sproingy made it to the ground !
  read more ... <>

More information about the Python-list mailing list