In python, tuples are considered immutable. However, I want to know if tuples can be modified by editing the computer's memory. In order to edit the computer's memory, I was thinking that I could invert the built in id function in order to write to that memory location the new tuple. I am curious about this because I want to know out of curiosity if tuples are really as immutable as they were designed to be.
1 Answer
This might be interesting example for you
t = (1, [2])
print(t)
try:
t[1] += [1]
except TypeError as error:
print("Error occured", error)
finally:
print(t)
Output:
(1, [2])
Error occured 'tuple' object does not support item assignment
(1, [2, 1])
3 Comments
dan04
Yep. Tuples are only as immutable as the objects inside them.
wim
This is just an obfuscated way of doing
t[1].append(1). It's not really in the spirit of the question.juanpa.arrivillaga
The tuple isn't being mutated here
PyTuple_SetItem, but don't tell anyone I told you about it.