I was just toying around in the interpreter and ran across something that I do not understand. When I create a tuple with a list as one the elements and then try to update that list, something strange happens. For example, when I run this:
tup = (1,2,3,[4,5])
tup += 
TypeError: 'tuple' object does not support item assignment
(1, 2, 3, [4, 5, 6])
This is actually documented in the Python docs.
EDIT: Here's a summary so that this is a more complete answer.
+=, Python calls the
__iadd__magic method on the item, then uses the return value in the subsequent item assignment.
__iadd__is equivalent to calling
extendon the list and then returning the list.
Therefore, when we call
tup += , it is equivalent to:
result = tup.__iadd__() tup = result
From #2, we can determine this is equivalent to:
result = tup.extend() tup = result
extendon the list, and since the list is mutable, it updates. However, the subsequent assignment fails because tuples are immutable, and throws the error.