Mutable & Immutable object in python

Rain Wu
Random Life Journal
3 min readApr 13, 2020

In python, a language with strong OOP feature, everything is an object. One of the classification methods to divided the objects into two groups is “mutable or immutable”, which represents whether an object can be modified after it was created.

Picture by Nader Elsayed on pinterest

This feature is worth discusssing, it is almost impossible to avoid the behavior of modifying objects in the program for developers. When we intend to modified an object, we need to know if it could be modified. Or, a more abstrct problem, which one actually changed?

It is a critical task for the beverage store to clearify which beverage should be sugar free, and which one should be full sugar, even though they are all beverage.

The id( ) function

When I was a newbie to python about three years ago, I take this function as a tool that will never be used, I can not understand why get-started books put it on the common-use cheatsheet page at all.

And I skip it without hesitation :).

But the software I developed became more and more complicated overtime, some weird behaviour of my code drive me to figure out the advanced object life cycle in python.

As the description on Python 3.8.2 documentation for id():

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Sounds interesting, let’s do some tests

In the code above, a string object “12” was created and assigned to variable x, then we glance their ids.

139933797597872
139933797597872

Maybe your object id will not be the same as mine, but it doesn’t matters. What we need to focus on is variable x store the object “12”, they are actually the same object because of the same id!

So, if I modify variable x, object “12” would be modified, right?

140564650636400
140564650636528

No, they are already not the same, a modified command already contruct a new object for x. That’s how immutable object works in python, once it was created, it can not be modified.

BTW, here’s another eye-opening behaviour:

and the output is…

139904208979056
139904208094320
139904208094320
139904219321408
139904219321440
139904219321440

It seems like Python prefer to reuse the immutable objects already created (a very economical act…), since they are immutable, there’s no floating state issue to worry about.

The mutable objects

Unlike the immutable objects, they accept valid modify even if they already been created. Common data type like list, dict, and set are all mutable.

140199618200112
140199618200112
140199617680000
140199617680000
140199616712288
140199616712288

We can find the object id unchanged after modification, the output indicates they still the same object. That’s why deep copy for an object is necessarily if you don’t wanna pollute the original one.

Nested structure

Let’s give a test about modifying a list within a tuple, which is a mutable object embedded inside a immutable one. In theory, tuple only ensure that objects it holds not changed, but any modification in the objects themselves won’t be prohibited.

140377141139592
140377162534280
140377141139592
140377162534280

Yeah, it’s valid for us to update an mutable element inside a immutable one without create a new object, this can be checked by object id.

Call by Reference

When we use Python to process large amounts of data, we are used to passing a List as a parameter to the function. The following script demostrate how the function will pollute the orginal object.

139937276418184
139937276418184
[1, 0]
139937276418184

The object id outside and inside the function are the same, it means the list we modify in function is actually the thing we use outside the function. In this case, if we use the data again for other operations, we will get a wrong result.

The pollution problem could be solved by a deep copy for the parameter:

139658206304392
139658206304456
[1]
139658206304392

A new object id with the object inside the function, it’s totally not related to the orginal object, no pollution will be reflected on the original object.

--

--

Rain Wu
Random Life Journal

A software engineer specializing in distributed systems and cloud services, desire to realize various imaginations of future life through technology.