Mutable and Immutable Objects in Python
In my post on hash tables, I encountered the concept of immutable and mutable data for the first time, and promised to write a post about it in the future. The future is here, and so we’re going to talk briefly about what mutable and immutable data types are in the context of Python. Many thanks to Lorraine Li’s excellent post on the topic which guides this blog post .
In our discussion of hash tables, we said that only immutable data structures could be hashed. We glossed over this topic by saying that immutable data is data whose value cannot be changed once it has been created. But today we’re going to go into more detail.
First let’s explain that in Python, different data types and structures can all be abstracted as objects . Python is an object-oriented language, where objects have properties (i.e. attributes) and some functionalities (i.e. methods) associated with them . In Python, you can use the methods via the dot syntax . For example, you can use the append method for a list object to add an item to a list like this:
But the surprising thing (at least to me) is that not only are more complex data structures like lists represented as objects - even basic data types like float or int have methods and attributes associated with them . So the assertion that “everything in Python is an object” is actually true, it’s not hyperbole .
Python distinguishes between objects whose value can change - mutable objects - and objects whose values cannot change - immutable objects . Let’s clarify what we mean by “value” in this context. Every object in Python has a unique ID, a type and a value . The ID is used by Python to retrieve the object from memory when we need it, and it never changes. Similarly, the type of the object never changes - it controls what operations the object can be involved in and what values can be assigned to it .
But the value of an object in Python can either be changed or it cannot, depending on whether the object is mutable or immutable . For example, an int object is immutable, but a list is mutable - let’s see how that plays out in an example below .
If we make an immutable int object called “year” and change the value of “year”, what happens? Did we change the value of “year”? Actually, we didn’t, as we can see when we check the ID of “year” before and after changing the value . When we changed the value of “year”, we actually just made the name point to a new location in memory storing the new value (that’s why the ID changed) .
But if we make a mutable list object and remove an element from the list, what happens to the ID of the list? As we can see below, the ID of the list stays the same even when we change the value . That’s what makes the list mutable, while the int is immutable.
Let’s get the breakdown of mutable and immutable data types in Python.
Mutable data types :
- Byte arrays (the mutable version of a byte object)
- Sets (Python has set and frozenset, they are both unordered collections of immutable objects, but only set is mutable)
Immutable data types :
- Numeric data types (i.e. bool, floats, ints, etc.)
- Frozen sets (immutable version of a set)
- Tuples (a sequence of arbitrary Python objects)
Okay, that was just a quick post, but hopefully it helped clarify some points on mutability and objects in Python.
 Li, L. “Immutable vs Mutable Data Types in Python.” Towards Data Science on Medium. 17 Oct 2019. https://towardsdatascience.com/immutable-vs-mutable-data-types-in-python-e8a9a6fcfbdc Visited 16 Jan 2021.
 VanderPlas, J. “Basic Python Semantics: Variables and Objects.” A Whirlwind Tour of Python. https://jakevdp.github.io/WhirlwindTourOfPython/03-semantics-variables.html#:~:text=Python%20is%20an%20object%2Doriented,Python%20everything%20is%20an%20object.&text=In%20Python%20everything%20is%20an%20object%2C%20which%20means%20every%20entity,accessed%20via%20the%20dot%20syntax. Visited 16 Jan 2021.