Sitemap

Python3: Immutable? Mutable? What?!?!

5 min readJan 27, 2017

--

Introduction

There are many data types to describe an object in python3. And all of them can be divided into two categories of mutable and immutable objects. What this means is that mutable objects can be altered after the object is created and immutable objects can’t be altered after the object is created.

Id and Type

So how you know if the object that you’ve created is immutable or mutable? Well you can use the type() function, which is built-in in python. Type() will return the type of the object. If the type is a int, float, long, complex, str, bytes, tuple, or frozen set than it’s probably an immutable object. However, just like anything in life, you should check your own source and confirm your findings. Once you create a variable store reference to the object, you should check it’s identity with the id() built-in function. Let’s look at some examples.

>>> a = [1]
>>> b = [1]
>>> type(a)
<class 'list'>
>>> type(b)
<class 'list'>
>>> id(a)
139749179448136
>>> id(b)
139749147140616
>>> a == b
True
>>> a is b
False

Here we initialized two variables, a and b that have the same value. We can also see that their types are the same, but have different id(). We use == to check if the all the references of the items stored within our objects a and b are the same. is check to see if the identity is the same.

Mutable objects

So mutable objects means are objects that you can be altered. A way of thinking about it is to think of mutable objects as a box in which you can fill with anything and remove things from. So when you go back and check on the box it will contain what you removed or added. Let’s look at another example.

>>> def increment(x):
... x[0] += 1
...
>>> a = [1]
>>> id(a)
139749147140552
>>> increment(a)
>>> a
[2]
>>> id(a)
139749147140552

We can see here that a is a list which is a mutable object. The value of a initially is [1] but after the function, increment, is called we see that the value store in our object, a has changed, but it still have the same identity before and after the function was called. So that is something you want to be careful of.

However, if you want to avoid altering your original object, we can create a copy of it.

>>> a= [1]
>>> b = a[:]
>>> id(a)
139749147151944
>>> id(b)
139749179448136
>>> a == b
True
>>> a is b
False
>>> b
[1]

This is something specific to lists, but you can see when we use [:] notation, what resulted was an object of the same value of a, but a different identity.

Immutable objects

So immutable objects are objects that can’t be changed. You can think of mutable objects as a clear box that you can only open once to put something in. You can see what is in there, but you can’t change what’s in it. A variable stores the reference to that object, so if you want to change the value within the object you must create a new immutable object that is different. So let’s look at an example:

>>> a = 1
>>> id(a)
10055552
>>> a += 1
>>> id(a)
10055584
>>> a
2
>>> b = [1]
>>> id(b)
139749147151944
>>> b += [1]
>>> id(b)
139749147151944
>>> b
[1, 1]

So the object a where we try to alter the value stored within the object, but we can see the identity before and after a was incremented is different. Because a is storing a reference to an immutable a must be set to a different immutable object in order to access it.

Get Richard Sim’s stories in your inbox

Join Medium for free to get updates from this writer.

If we look at the variable, b it is storing a reference to a list object. Lists are mutable, thus b can hold the same reference to the object that can be changed.

How does Python treats mutable and immutable objects differently and why does that matter?

So let’s get in to the nuances of python. Mutable and immutable objects are handled differently by python. First to say it matter because you need to understand what is expected. When it come to immutable objects, the creator of the code intended to have this code prevent being messed with. Immutable object are quicker to access than mutable objects. Let’s take a look at an example of why this matters.

>>> a = b = [1]
>>> a
[1]
>>> id(a)
139749186966416
>>> id (b)
139749186966416
>>> a += [2]
>>> a
[1, 2]
>>> b
[1, 2]
>>> id(a)
139749186966416
>>> id (b)
139749186966416
>>> a = b = (1,)
>>> a
(1,)
>>> id(a)
139749147140552
>>> id(b)
139749147140552
>>> a += (2,)
>>> a
(1, 2)
>>> b
(1,)
>>> id (a)
139749186943688
>>> id(b)
139749147140552

Here we are executing the same operation with an mutable object, a list, and an immutable object, a tuple. We can see that a and b are set using alias, so that the the ids will be the same. However with an immutable object the id will be changed to thus making the id of a and b of immutable objects different as shown in the example above.

So python knows to treat mutable objects dynamically(able to change) and immutable objects as static(not able to change)

How arguments are passed to functions and what does that imply for mutable and immutable objects

So, let’s see what happens when you pass an argument to a function if the argument is list.

>>> def assign_value(n, v):
... n += v
...
>>> l1 = [1]
>>> l2 = [2]
>>> assign_value(l1, l2)
>>> l1
[1, 2]
>>> l2
[2]
>>> def assign_value(n, v):
... n = v
...
>>> l1 = [1]
>>> l2 = [2]
>>> assign_value(1,2)
>>> l1
[1]
>>> l2
[2]

We can see that depending on the function we l1 and l2 start as the same values, but than when we that call l1 and l2 they have two different outputs after their respective functions. So when you enter the scope of a function, if you modify an the object that is originally reference by the argument of the function, it will change the mutable object. As seen by the first example above. This is a question of if you’re modifying and object or assigning it. In the first example we are modifying the list that is being pasted in. In the second example we are assigning the first l1 to l2 and that doesn’t change l1 out side of the function.

So when dealing with immutable object, you can’t change the object that is being referenced.

>>> l1 = (1,)
>>> t1 = (1,)
>>> t2 = (2,)
>>> def assign_value(n,v):
... n = v
...
>>> assign_value(t1, t2)
>>> t1
(1,)
>>> t2
(2,)
>>> t1
(1,)
>>> t2
(2,)
>>> def assign_value(n,v):
... n += v
...
>>> assign(t1, t2)
>>> t1
(1,)
>>> t2
(2,)

If we pass a tuple into the same functions into the same examples in the one above, we get can see that t1 and t2 remain the same.

Conclusion

So we can see the importance of immutable and mutable objects. The different ways that python treats them and how important it is to understand how the values assigned to a variables can share, or have difference references to them.

--

--