I am trying to understand the following error in python:
In []: tup = tuple(['foo', [1,2], True])
In []: tup[1]+=[2]
This results in the following error:
TypeError: 'tuple' object does not support item assignment
I understand this because tuples are not mutable. However, the second element of the tuple is now mutated as desired.
In []: tup
Out[]: ('foo', [1, 2, 2], True)
Can someone explain what happened here. I got an error, but the code did exactly what I wanted. What failed to execute the code that displays the error?
Also, I understand that the append method can do the same.
In []: tup[1].append(2)
In []: tup
Out[]: ('foo', [1, 2, 2, 2], True)
But I still want to understand what happened in the first case above.
source
share