Hey guys!! Interested to know something about Mutability and Immutability. Let’s it check out!
A mutable object is a changeable object that can be modified after it is created. Let’s get it straight using an example.
# Listlst = [1,2,3,4]# printing listprint("The list is: ", lst)# printing the id of listprint("The id of list is: ", id(lst))# mutating second element of listlst[2] = 4# printing listprint("The list after change is: ", lst)# printing the id of listprint("After changing the id of list is: ", id(lst))
What have you observed?
Two things, right?
The list
and its id
before and after changing the list.
The list
in these two cases is different, but the list id
is the same.
Now, the question is, what is this id
?
It corresponds to the object’s location in the memory and is specific to Python implementation.
Let’s check out practically:
# Integera = 6print("The id of 'a' is: ", id(a))# Stringstr = "teacher"print("The id of 'str' is: ", id(str))# Listlst = [1,2,3,4,5,6]print("The id of 'l' is: ", id(lst))
From this, you can observe that ids
exist lists and all objects.
An immutable object is an object whose state can never be modified after it is created.
# Integera = 6# printing before mutatingprint("Initial id: ",id(a))# mutating interger `a`a = 7# printing after mutatingprint("Id after changing: ",id(a))
The same variable a
has multiple id's
, right?
Here the id
represents an integer that corresponds to the memory location where a value is stored.
Remember that is stored at 10919488
and, after it is changed to , the memory location is no longer the same.
Hence, is created in another memory location because variable a
is immutable.
Let’s check out some examples that will enable us to discuss various scenarios.
# Integera = 6# printingprint("Id of 'a' variable: ",id(a))# no mutationb = a# printingprint("Id of 'b' variable:",id(b))
In the above example, what are the ids of a
and b
? Both are same!!
The reason is that both a=6
and b=6
point to the same memory location.
Let’s check out another example:
# Integera = 6# printing id of 'a'print("a:", id(a))# printing id of '6'print("6:", id(6))# no mutationb = aprint("b:",id(b))# mutatinga = a + 1print("a:",id(a))print("b:",id(b))
In the first two lines, the id of a
and b
are the same, but in the last two lines, the id of a
is different from the id of b
.
What could be the reason?
Mutable objects: list, dict, set, byte array
Immutable objects: int, float, complex, string, tuple, frozen set, bytes
– Until now, based on our discussion, we thought that immutable objects will never change their state, size, or the stored value.
– This is true for int, float, string, etc.
– A Tuple is considered immutable since, unlike its other counterparts, its state never changes.
– But a tuple is a mixture of both immutable and mutable objects.
– In a tuple, we might not change an immutable object(e.g., string), but we can change a mutable object (e.g., list).
See the example below :
t =("name", [1,2,3,4])# here we cannot change the "name" and add additional string to it# but we can change the list in the tupleprint(t) # before change of listprint(id(t))t[1][3] = 5print(t) #after change of listprint(id(t))t[1].append(6) # we can increase the internal size of listprint(t)print(id(t))
From the above code, we can observe that the overall size of the tuple isn’t changed since it’s immutable and there is no additional element being added to the tuple.
But we are changing the internal element list by changing the existing values or elements and adding new ones.
Externally a tuple looks immutable.
Ok, we are done with it!!
So what have we learned…
– Python handles mutable and immutable objects in different ways.
– Mutable objects are of great use in scenarios where there is a need to change the object size (e.g., list, set, and so on…).
– Immutable objects’s are used when you want an object you created to always stay the same as it was when created.
“Immutable objects are accessible in a quicker way than mutable objects.”
“Changing Immutable objects is fundamentally expensive as it involves creating a copy; but, changing mutable objects is considered cheap.”
Free Resources