1

I know that tuple is an immutable object and we can't change it after it is created,but in my code I should explain why and why not I can do following codes

t = (1, 's', [] )
t[2].append(-1)
print(t)

and it outputs

(1, 's', [-1])

So basically I did change the tuple and it did not give out an error How can I give an explanation to it?

Narmin
  • 69
  • 1
  • 8

1 Answers1

2

A tuple is likely a set of memory addresses where each address is immutable, and when you get a index into the tuple it returns a memory address to the object (In this case python obscures this from your view so you don't have to worry about it) so when you do .append on the list as a index in the tuple you change the list and aren't affected by the immutability of the tuple