copy on write

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Thu Feb 2 11:42:01 CET 2012


Am 13.01.2012 13:30 schrieb Chris Angelico:

> It seems there's a distinct difference between a+=b (in-place
> addition/concatenation) and a=a+b (always rebinding),

There is indeed.

a = a + b is a = a.__add__(b), while

a += b is a = a.__iadd__(b).

__add__() is supposed to leave the original object intact and return a 
new one, while __iadd__() is free to modify (preference, to be done if 
possible) or return a new one.

A immutable object can only return a new one, and its __iadd__() 
behaviour is the same as __add__().

A mutable object, however, is free to and supposed to modify itself and 
then return self.


Thomas



More information about the Python-list mailing list