Immutable objects in python
- What is immutable?
Immutable means which can not be changed.
- We know that once the Object created we cannot modify. In case we perform modification then new object gets created with changes.
- In python, everything in python is an object. All fundamental data types are immutable.
X = 10
X is int type of Object.
When the above statement executed python creates an Integer Object.
we change the value of X
X = 101
Now when we change the value of X = 101. python will create new Object and X will point to a new Object.
Why this immutable concept in python?
- Consider one example,
X = 10 For this python will create Object with value 10
Now, Y = 10 another variable with the same value
In this case, python will look for value first. The object is created with value 10, X is pointing to Object so instead of creating new Object with the same value python gives Y reference to the same Object.
As shown in the above diagram X and Y are now will point to the same Object.
- In python, Object immutable because object reused which will improve performance as well as memory utilized.
An immutable concept may be a problem or an advantage?
- Example: Consider the voter registration system
In the voter registration system, there may be any number of voters.
For voters v1 if location = “Hyderabad”
Then Python will create object v1 with location Hyderabad
Voter v2 = “Hyderabad”
When voter v2 having the same value as v1 python will check for value with the same object. If it finds then instead of creating an object it created a reference to an existing object.
For 1 caror voter it creates 1 caror reference instead of creating 1 caror object.
- This will increase performance and memory utilization.
- Note: –
- Python follows this approach, whenever creating new object python not creates an object directly it checks for an object if it is already available it will use an existing object, it will never go create a new object for performance.
- There is no limit for creating the reference.
Advantage of immutable object
- Memory utilization
Disadvantage of reference
- Consider the example of the voter registration system and we are having records of 1 caror with location = ” Hyderabad”
All the voters from v1 to v1caror pointing to the same object (Hyderabad object)
- Now if v2 voter changed its location value =” Hyderabad ” to location=” Pune”, then Hyderabad object will change to Pune
We can see what happened after changing the voter v2 value. All other reference value also become Pune which is not right.
It is always a bigger problem.
- How to overcome this problem?
So to overcome this problem, python will not allow changes to the existing object ( because there may be a number of references) instead it creates a new object and the existing reference ( modified) will point to a new object.
To overcome this Immutability concept is provided in python.
- Why Immutability of the object is required?
In python, same objects can be used by multiple references if one person trying to change the content remaining references also effected to prevent that is one reference changed existing value it should not change the existing object it will create a new object and new object is used.
- Note:- All the fundamental data types in python are immutable.