Python mutable object or immutable object

What does mutable object and immutable object mean in Python? In this post, we will discuss all the ins and outs of the mutable object and the immutable object.


Python mutable objects

The mutable objects are those objects whose value can be changed. For instance, any object of list type belongs to a mutable type.

A list object can have many values, and since the object is of mutable type, any of the value can be changed. Consider the code below.

>>> ls=[23 , 89 , 90]
>>> ls[2]=890 #Changing the third value
>>> ls
[23, 89, 890]

Some of the mutable object type are bytearray , set , dictionary ,etc. An example is given below which shows that value of mutable objects can be changed.

>>> #Changing bytearray value
>>> s='New string'
>>> b=bytearray(s ,'utf-8') #Creating bytearray object
>>> c=bytearray( 'G' , 'utf-8' ) #'c' is also bytearray object
>>> #Replacing the first value of 'b' with the first value of 'c'
>>> b[0]=c[0] 
>>> b
bytearray(b'NeG string')
>>> #Changing set object value
>>> st={ 10 , 11 , 12 , 13 }
>>> st.update( [23] ) #Adding new value to set
>>> st
{10, 11, 12, 13, 23}

Try changing the value of dictionary object.



Immutable object

Immutable object are those object whose value cannot be changed. Some of the immutable type are tuple, byte , frozenset , string , integer , floating point type ,etc.

If you have used string or int or floating point type in your program before, you might have noticed that we can change the value of integer object or floating point object. We can also elongate the string in whatever ways we desire. Consider the code below.

>>> s='New'
>>> s='New'+ ' string' #Chnaging the value of 's'
>>> s
'new string'
>>> #Integer object
>>> n=900
>>> n=19010
>>> n
19010

In the above code, we could easily change the value of ‘s’ and ‘n’ object, then why are they considered an immutable object. The explanation is given below.


Best way to check for mutability or immutability

In the above section we have seen that string and integer object despite being an immutable type their values can be changed, so what does this mean?

First thing to note, in the above example when changing the value we are not literally changing the object’s value, we are rather creating a new object with the same name but with new address and containing the new value.

Second thing to note, when the mutable object content is changed the address is not changed.

Keeping the two points in mind we can conclude that if any object changed their address when their content is changed, then they are not qualify to be mutable type.

We can easily check if their address is changed or not using the function id(). How to use the id() function is shown below.

>>> ls=[23 , 89 , 90 , 12] #list object
>>> id( ls ) #return the address of 'ls' which is given below
20953408
>>> ls[3]=1000 #Changing the value of ls[3]
>>> ls
[23, 89, 90, 1000]
>>> id(ls)
20953408 

After changing the ls[3] value to 1000, the address returned by id(ls) is still the same. This shows that list is a mutable type.

Now let’s try it out on immutable object.

>>> i=9101 #integer object
>>> id(i)
20372896
>>> i=111 #change the value of 'i'
>>> i
111
>>> id(i)
1710280544

When ‘i’ value is 9101 we get its address as 20372896 , but after changing ‘i’ value to 111, we get its address as 1710280544. The address is changed! This is why integer objects are immutable.

Try using the id() function on string, set, floating point objects.

There are some distinct differences between the mutable object and immutable object besides their changeable or unchangeable nature, they are discussed in another post.

Link : Python difference between mutable and immutable objects