Python: Mutable vs Immutable Objects

Onsjannet
5 min readSep 29, 2020

As we covered in a previous article everything in python is an object. To get started with this programming language you have to know that objects can either be mutable or immutable.

Every variable holds an object instance that’ll later on and during initiation will be assigned. so what’s the difference between mutable and immutable?

  • mutable objects : are objects that could be changed like “ list, set, dict”
  • immutable objects: are unchangeable objects like “int, float, bool, str, tuple, unicode”

So now you know what mutable and immutable objects are how do you think we can distinguish between them. Well it’s easy with the help of the “ ID” and “ TYPE”.

what’s an ID and what’s a TYPE :

  • ID : id() is a built-in python function that returns the identity of an object as an integer. The identity returned is a unique and constant one it corresponds to the object’s location in the memory. to compare the identity of two objects we can use is.
  • TYPE: type() is built-in python function that returns the type of an object. We can create many types of variable, and see all the different classes they can belong to.

Example :

''' Example 1 '''
>>> x = "Holberton"
>>> y = "Holberton"
>>> id(x)
140135852055856
>>> id(y)
140135852055856
>>> print(x is y) '''comparing the types'''
True
''' Example 2 '''
>>> a = 50
>>> type(a)
<class: ‘int’>
>>> b = "Holberton"
>>> type(b)
<class: 'string'>

And that’s how you compare a string variable to find out the types and ids. And that’s how we check how objects are associated with variables and can they be changed.

Mutable and Immutable Objects:

As i mentioned previously objects can either be mutable like lists, dicts, sets, byte array.. or immutable like int, float, complex, string, tuple, frozen set [the immutable version of set], bytes. Now let’s take examples:

i = 10
i = j

Now let’s create an object of type int knowing that both the identifiers i and j points to the same object.

id(i) == id(j)
id(j) == id(10)

However when doing a simple operation:

i = i + 1
id(i) != id(j)
id(i) != id(10)

The object in which i was tagged is changed. Object 10 was never modified for it’s an Immutable object.

let’s try now a mutable object in our case a list

m = list([1, 2, 3])
n = m
id(m) == id(n)

let’s try the function pop() to remove the item at the given index from the list and returns the removed item

m.pop()
id(m) == id(n)

m and n points to the same list object after the modification it will now contain [1, 2].

Why does it matter and how differently does Python treat mutable and immutable objects :

Now we know the difference between mutable and immutable objects we know that immutable objects can be used to make sure that the object remains constant throughout the program. How ever the mutable objects can be changed whether expected or not. Changing a mutable data type won’t change it’s memory address on the other hand changing an immutable type can produce errors.

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

Compiling function arguments in Python changed based on the function arguments type ( Mutable or immutable).

when mutable objects are being called by reference in a function the variable itself might change however this can be avoided by creating a copy. On the other hand when immutable objects are called being called by reference in a function their values remain the same and it cannot be changed

Passing Immutable Objects:

>>> def increment(n):
... n += 1>>> a = 3
>>> increment(a)
>>> print(a)
a = 3 # a is still referring to the same object

Let’s decompose this:

the variable is refers to the object with the value 3 (a = 3)

passing a to increment(n), the function refers the local variable n to the same object

let’s pass to the n += 1 par. We previously mentioned that integers are immutable objects that makes them impossible to modify so we can’t change the 3 to a 4 with this being said we created a new object with the value 4.

And that’s why the value of a didn’t change even after using increment().

Passing Mutable Objects:

>>> def increment(n):
... n.append([4])>>> L = [1, 2, 3]
>>> increment(L)
>>> print(L)
L = [1, 2, 3, 4] # a changed!

Let’s decompose this:

As mentioned before lists are mutable L = [1, 2, 3] refer to a list object that refer to “3" integers 1, 2, and 3.

when passsing L to increment(), the function makes the local variable n refers to the same object as L

using the .append() method will modify the list. When L is printed we get a midified listsince no object was being created and the changed occurred in place of the object.

And with this being said that’s the diffrance between mutable and immutable objects and how arguments are passed to functions.

--

--