Question Details

No question body available.

Tags

python tuples immutability python-internals augmented-assignment

Answers (5)

February 5, 2026 Score: 3 Rep: 46,119 Quality: Low Completeness: 40%

Yours is a good question. Note that the following works with no problem:

>>> t = (1, 2, [30, 40])
>>> arr = t[2]
>>> arr += [50, 60]
>>> t
(1, 2, [30, 40, 50, 60])
>>> t[2] is arr
True

Given that t[2] is arr is True, t[2] += [50, 60] should have worked in my opinion. I would like to know why this is not a compiler bug or a deficiency in the language specification.

February 5, 2026 Score: 3 Rep: 165,337 Quality: Low Completeness: 30%

There is a specific official Python FAQ for this question: Why does a_tuple[i] += [‘item’] raise an exception when the addition works?

February 5, 2026 Score: 1 Rep: 165,337 Quality: Low Completeness: 50%

In particular the += statement in the question destructures to

t[2] = t[2].iadd([50, 60])

and the iadd special method is not guaranteed to return self. If it does return something different then the contents of the tuple would need to change, but the tuple is immutable.

The only part of this that isn't spelled out in the linked FAQ is that iadd might return a different object, but otherwise there's a lot of detail there.

February 5, 2026 Score: 0 Rep: 46,119 Quality: Medium Completeness: 60%

Assuming that the statement ...

t[2] += [50, 60]

... is equivalent (or as you say destructures) to ...

t[2] = t[2].iadd([50, 60])

... and I have no reason to doubt you, then I still would claim that what Python is doing is wrong and this statement should instead be equivalent to just ...

t[2].iadd([50, 60])

... without the additional assignment back to t[2]. So, it shouldn't matter what the result returned by iadd is in this particular case because there is no reason for that result to be assigned back to t[2] in my opinion. Where it would matter would be in the case where the result of iadd is being assigned explicitly, for example with:

my_result = t[2].iadd([50, 60])

But in this case iadd seems to be returning self, which is the object on which iadd is being called on, i.e the list and not the tuple, for whatever that is worth.

February 5, 2026 Score: 0 Rep: 46,119 Quality: Low Completeness: 50%

It basically states that for the statement atuple[0] += 1 where atuple[0] is a list instance, that what Python does is essentially the following:

result = atuple[0] + 1
atuple[0] = result

But I believe that the second statement, i.e. atuple[0] = result should not be generated at all. In the OP's case atuple[0] is a reference to a mutable list and it is this list that is being updated by a_tuple[0] += [50, 60], not the tuple.