Skip to content
Advertisement

Why true immutability is impossible in Python?

I was reading the documentation for attrs. It says:

Please note that true immutability is impossible in Python

I am wondering what is the reason for that. Why someone cannot have an immutable list in Python while it is possible in C++? What is the main difference here?

Advertisement

Answer

TLDR; “True Immutability” is only possible on an impervious stone tablet, but it’s counter-productive to the discussion of mutability, and why it is used / is important. It’s not worth being technically correct at the expense of being practically wrong.

This is a bad argument of semantics. Python allows re-defining variable names with different types in an otherwise strongly typed language, which is where some of the confusion comes from, but to be clear the object a variable name refers to can very much be properly immutable.

Take for instance a tuple with a few numbers in it:

>>> tup_A = (1,2,3)

It is not possible to change the values of any of the objects in the tuple:

>>> tup_A[0] = 10
TypeError: 'tuple' object does not support item assignment

It is possible to overwrite the variable name tup_A with some other value, but then it will be a different object entirely even if it was related to the original. For example a slice of a tuple creates an entirely new object rather than a view of the original:

>>> id(tup_A)
2887473131072
>>> tup_A = tup_A[:1]
>>> id(tup_A)
2887473037616

I believe the article mentioned may also be somewhat referring to the possibility of creating custom immutable types (classes). This is also a bad argument because there are plenty of mechanisms to enforce immutability. In particular, the tools for customizing attribute access, and the @property function can be used to great effect for this. Once these methods are used to implement immutability, one would have to intentionally break the class to mutate data which was not meant to be mutated. This is of course possible because python is primarily distributed as source code, but the same could theoretically be said for the python c api. Tuples don’t have to be immutable if you re-write python, but that’s so far beyond the point, it’s fair to say it’s just wrong.

Immutability is a tool with a specific purpose. It is a good idea to use it whenever possible so an accidental slip-up will produce an error message rather than a silent bug. If you encounter errors like this, you should never ask “how can I mutate this value which was intended to be immutable?”, but rather ask “why is this value not meant to be mutated, and how am I intended to utilize it?”

P.S. You could probably even mutate a tuple without editing cpython using the ctypes library by getting the actual memory locations of the objects contained within it, and overwriting the pointers, but this would break lots of things (like garbage collection ref counting). Don’t do this. It’s another one of those “so far beyond the point” things.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement