copy on write

Steven D'Aprano steve+comp.lang.python at
Fri Jan 13 08:04:16 EST 2012

On Fri, 13 Jan 2012 23:30:56 +1100, Chris Angelico wrote:

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

Actually, both are always rebinding. It just happens that sometimes a+=b 
rebinds to the same object that it was originally bound to.

In the case of ints, a+=b creates a new object (a+b) and rebinds a to it. 
In the case of lists, a+=b nominally creates a list a+b, but in fact it 
implements that as an in-place operation a.extend(b), and then rebinds 
the name a to the list already bound to a. 

It does that because the Python VM doesn't know at compile time whether 
a+=b will be in-place or not, and so it has to do the rebinding in order 
to support the fall-back case of a+=b => a=a+b. Or something -- go read 
the PEP if you really care :)

Normally this is harmless, but there is one interesting little glitch you 
can get:

>>> t = ('a', [23])
>>> t[1] += [42]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t
('a', [23, 42])

> which is sorely
> confusing to C programmers. But then, there's a lot about Python
> that's sorely confusing to C programmers.

I prefer to think of it as "there's a lot about C that is sorely 
confusing to anyone who isn't a C programmer" <wink>


More information about the Python-list mailing list