Tuples are Immutable, except when they're mutable.

Humpty Dumpty said that when he uses a word it means just what he chooses it to mean.

"immutable" means the value cannot change in Python.These are the basics of Python.Chapter 2 of Luciano Ramalho's excellent "Fluent Python" explains that tuples are more than just lists.Luciano wrote a great post on this topic.

We need to get a nuanced answer to the question.We need some background information.

All data in a Python program is represented by objects or relations between objects according to the data model.A value in Python is an object.These are considered separate from objects in Java.In Python, it's not true.Ned Batchelder's PyCon 2015 Facts talk and myths about Python names and values goes into more detail about the fact that every value in Python is an object.

The datetime object is an object, but the integer 42 and the true are objects.

There are three things that Python objects have: a value, a type, and an identity.This is a bit confusing, because we often casually say, for example, "the value 42, although 42 is also called an object which itself has a value."Let's continue with our 42 example.The following can be entered into the interactive shell.

There is an object with a value of 42, a type of int and an identity.An identity is a unique number that never changes for the lifetime of the object.An object's type can't be changed.The value of an object is the only thing that can change.

You may think you've changed the object's value, but you haven't.All you did was refer to a new object.You can confirm this by calling the id function.

The value of the strings, floats, and frozen sets doesn't change.There are lists and dictionaries that are mutable.This can lead to a Python gotcha.

The reason eggs have changed is because they refer to the same object.The egg line made a copy of the reference.If you want to copy a list object, you can use the copy module's functions.

I've found that the official Python documentation describes tuples as immutable.The glossary says "mutable".

The is operator compares identities with the equality operator.The shorthand for id(x) is x is y.The following can be entered into the interactive shell.

According to the official Python documentation, if the object has a hash value which never changes during its lifetime, it's a hashesable object.There are other requirements for the special methods, but that is beyond the scope of this post.

When an object has the same value, it always has a same hash.There will occasionally be the same hash for objects with different values.This is called a collision.The object's identity is not the only factor that can be used to calculate the value of the function.

mutable objects can't be hashable.For reasons beyond the scope of this post, only hashable objects can be used as keys in a dictionary.This means that the object's hashes won't change during its lifetime since they are based on values.

The interactive shell can be used to create a dictionary with objects for keys.

All of the keys are valid.If you try to use a mutable object for a dictionary key, you'll get an error.

It seems that a mutable object can't be hashed.Raymond Hettinger explains why mutable values can be contained in immutable tuples.This is in line with what we know: immutable objects can be hashesable, but this doesn't mean they're always.The hash is derived from the object's value.

There is a corner case where a mutable list cannot be hashed.If the list's value can change, that means the value of the tuple can be changed as well.If we go back to the definition in the glossary, a hash is never supposed to change during the object's lifetime.

We should ask if mutability is a property of data types or objects.

Python programmers often say that string objects are mutable, which makes us think that mutability is a property of types.I agree.

In the previous section, we saw how some tuples are mutable but not others.

The official definition of a mutable object is "An object with a fixed value".

It is possible that mutability is a property of objects, and that some tuples contain one or more mutable objects.Even if they're unhashable, every Pythonista I've run into says that the tuples are still valid.Why?

The objects in a tuple cannot be replaced or deleted.It doesn't change the 42 object in the email; it replaces it with a new object, 99.The interactive shell can be used to look at a list.

The identities of the objects will always be in the same order.Tuples are unchanging.

Tuples are mutable because their values can be changed.The following can be entered into the interactive shell.

The tuples referred to by and b have the same values but are different objects.Let's change the list.

The a's value has been changed.We didn't change b's value because a is no longer equal to b.Tuples are not fixed.

Humans are masters of words.Humans invent words to convey ideas.I will continue to say that tuples are immutable, because this is the most helpful term in most contexts.

From our definitions of "mutable" and "value", we can see that values can sometimes change.It is possible to mutate.Tuples can be mutable, though it will raise eyebrows and require more explanation.

I would like to thank you for sitting through my lengthy explanation on tuples and mutability.I hope I conveyed what I learned to you in the post.