I just came across something that was quite strange.
>>> t = ([],)
>>> t[0].append('hello')
>>> t
(['hello'],)
>>> t[0] += ['world']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t
(['hello', 'world'],)
Why does it raise TypeError and yet change the list inside the tuple?
As I started mentioning in comment,
+=actually modifies the list in-place and then tries to assign the result to the first position in the tuple. From the data model documentation:+=is therefore equivalent to:So modifying the list in-place is not problem (1. step), since lists are mutable, but assigning the result back to the tuple is not valid (2. step), and that’s where the error is thrown.