[docs] [issue16701] Docs missing the behavior of += (in-place add) for lists.

Ezio Melotti report at bugs.python.org
Thu Jan 24 04:37:15 CET 2013


Ezio Melotti added the comment:

> What is "when possible" supposed to mean here?

Generally it means "when the object is mutable":
>>> l = [1,2,3]
>>> id(l)
3074713484
>>> l += [4]
>>> id(l)
3074713484
>>> t = (1,2,3)
>>> id(t)
3074704004
>>> t += (4,)
>>> id(t)
3075304860

Tuples are not mutable, so it's not possible to modify them in place, and a new tuple needs to be created.
Note that while most mutable objects in the stdlib that support += do indeed modify the object rather than creating a new one, I don't think this is strictly required.
IOW that paragraph is already warning you that (with mutable objects) the object might be reused, depending on the implementation.  Maybe this should be clarified?
(IIRC in CPython it could be possible that in some situations an immutable object still has the same id after an augmented assignment, but, if it really happens, it is an implementation detail and shouldn't affect semantics.)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16701>
_______________________________________


More information about the docs mailing list