Issue 7910: immutability w/r to tuple.add (original) (raw)
Issue7910
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) * ![]() |
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) * ![]() |
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:57 | admin | set | github: 52158 |
2010-02-11 17:45:27 | rhettinger | set | resolution: not a bug |
2010-02-11 15:40:19 | mattrussell | set | status: open -> closedmessages: + |
2010-02-11 15:32:40 | tim.golden | set | nosy: + tim.goldenmessages: + |
2010-02-11 15:28:21 | exarkun | set | nosy: + exarkunmessages: + |
2010-02-11 15:26:27 | mattrussell | create |