This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: immutability w/r to tuple.__add__
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 2.7, Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: exarkun, mattrussell, tim.golden
Priority: normal Keywords:

Created on 2010-02-11 15:26 by mattrussell, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg99219 - (view) Author: Matthew Russell (mattrussell) Date: 2010-02-11 15:26
Tuples, as we know are designed to immutable.

Hence I'm questioning if the following behavior is considered a defect:

>>> t = (1, 2)
>>> t += (1, 2, 3)
>>> t
(1, 2, 3)

?
msg99220 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-02-11 15:28
Your output looks fishy.  Anyway, the behavior of += isn't a bug:

>>> a = b = (1, 2)
>>> a += (1, 2, 3)
>>> a
(1, 2, 1, 2, 3)
>>> a is b
False
>>> 

It's confusing, to be sure, but no mutation is going on.  += is only in-place if applied to something that supports mutation this way (by implementing __iadd__).
msg99221 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-02-11 15:32
Just think about it for a minute:

t = (1, 2)
print id (t), t
t += (1, 2, 3)
print id (t), t

Not mutating, merely creating a new new object
and giving it the same name
msg99225 - (view) Author: Matthew Russell (mattrussell) Date: 2010-02-11 15:40
Yes, the output is fishy indeed my bad (paste error).

Tim: I hadn't thought for long enough or thought to check with the id builtin - nice catch.
History
Date User Action Args
2022-04-11 14:56:57adminsetgithub: 52158
2010-02-11 17:45:27rhettingersetresolution: not a bug
2010-02-11 15:40:19mattrussellsetstatus: open -> closed

messages: + msg99225
2010-02-11 15:32:40tim.goldensetnosy: + tim.golden
messages: + msg99221
2010-02-11 15:28:21exarkunsetnosy: + exarkun
messages: + msg99220
2010-02-11 15:26:27mattrussellcreate