I encountered to this example about python mutable and immutable:
t = (1, 2, [3, 4])
t[2] += [5, 6]
This will raise error:
TypeError: 'tuple' object does not support item assignment
Until here I can understand the error because t is a tuple and tuples are immutable, but problem is t
changed.
print(t)
# Output (1, 2, [3, 4, 5, 6])
an other thing is attempt to change t[2] by using append
or extend
has no error and works fine.
t[2].extend([7,8])
t[2].append(9)
print(t)
# Output (1, 2, [3, 4, 5, 6, 7, 8, 9])
Can anyone explain this?