Published July 29, 2025
Way back in the day I took a Python class given by Reuven Lerner.
He taught me the basics, since this was an intro class for programmers, including that a tuple is an immutable
object in Python. Today he posted a video to Bluesky
about what that actually means. Me, being the troublemaker that I am, asked "If a tuple contains a list and I mutate
the list did I mutate the tuple?", which is more of a philosophical than technical question. We continued the
conversation a bit and he pointed out something I didn't know - there you can kinda, sorta mutate a tuple.
>>> t = (1, 2, 3)
>>> t1 = t
>>> t == t1
True
>>> t is t1
True
>>> t1 += 4,
>>> t1
(1, 2, 3, 4)
>>> t
(1, 2, 3)
>>> t == t1
False
>>> t is t1
False
You can use the tuple's __add__ method to create a new tuple with an additional value. So in the case above
t doesn't change when I add something to t1 - the tuple I created on the first line has not changed - but the
tuple that t1 points to does contain the additional element. So while the object didn't mutate, the variable
effectively did.