Open In App

Why do we Need Immutables in Python ?

Improve
Improve
Like Article
Like
Save
Share
Report

When a Novice steps into the Programming world and kicks off to learn about different concepts of it and yet eventually reaching to the Data Structures and Algorithms, learning and implementing them, but one or the other way he/she tends to read once and forget about the Immutable object.

Mutable and Immutables exists in every programming language, yet one only tends to care about the mutable, like lists, queues, etc and take no or little notice of the immutables because at first impression it seems to be just an add-on and doesn’t seem to have any real-life problem-solving applications, and to debunk this idea, let us discuss the concept, need of it and Limitations.

What are Immutable Objects?

Mutability is a differentiating property of a data types in Python that makes a big contrast from the other data types, It tends to be an ability of data types to allow being modified after it is created, to which a value can be added as well as can be popped out of it. On the other hand, looking on the other side there are objects too that don’t follow this principle and are unalterable and don’t allow any modification to it after it’s defined. Its State cannot be changed whatsoever, It tends to represent a constant value once initialized. Examples – integer, complex, string, float, Tuple, Complex, Frozen set.

Therefore if any variable has initialized a value corresponding to any of these immutable data types, one cannot change it ever. To ever change it, one has to initialize the same variable to the modification one wants. When a variable is reassigned to some other string it tends to allot a different memory location for both the objects.

Example:

Python3




# string initialized
var = 'Geeks'
print(id(var))
print(var)
  
# Reassigned to another value
var = 'For Geeks'
  
# New Memory Location assigned
print(id(var))
print(var)


Output:

139758810541392
Geeks
139758782345520
For Geeks

Note: In Python there tends to be an exception in case of tuple’s immutability as the tuples themselves is an immutable yet cannot be modified after it’s initialized and the values it is given at the time of initialization be the final values it holds, nothing can add or delete value to/from it, However, a mutable field like a List embedded in a tuple can be modified without any error, and yet it proves that objects referenced by the tuple can be modified, this phenomenon is occasionally called “non-transitive immutability”.

In Python, coders have more advantage of optimization around the property of immutability, mainly for the string objects.

Example:

Python3




var1 = 'GFG' 
var2 = 'GFG'
var3 = 'GFG'
var4 = 'GFG'
var5 = 'GFG'
  
# All the variables points to a
# single String
print(id(var1), id(var2), id(var3),
      id(var4), id(var5))


Output:

140080377558384 140080377558384 140080377558384 140080377558384 140080377558384

If one tends to create 20 string objects one after the other, holding up same values, Then Python won’t be allocating different memory locations for each value, yet it’ll make every identifier to refer to the same string as it will not ever be modified further, Thus Saving lots of memory. The exception being that the same scenario won’t be applicable for every immutable object other than a string, yet this optimization trick proves to be Implementation Dependent.

Why care about Immutables ?

  1. Improves the Exactness and simplicity of the whole code, as it provides an ease to the coder to pass around the object in the program without any fear as it never seems to be ever modified. whereas the mutable is much harder to reason about. In mutable, the aliasing leads to many irregularities and eventually intimidating fidelity of the code, At the end resulting in variability.
  2. Thread Safe – As an object after the assignment cannot be modified, this deduces that a read-only data is being shared among the threads, which certainly provides thread-safety. In simpler words, In immutables, since there is no scope of change for an object, there is no need to be scared of accessing it from many threads. Mutation in any sense in immutable objects can be achieved in such a way that one creates a new object instead of trying to modify existing ones.

Limitations

Over time, different conclusions get introduced with it, as we read that Immutables is Thread safe, and no matter which thread reads their values, They get the right values, but The immutables are found to be immune to “Memory Consistency Errors”, that can be furthermore explained as; Immutable objects by themselves are not thread-safe. It is the code that uses them that must be written to be thread-safe. Simply using immutable objects is not enough to achieve this. One has to also guard against deadlock, livelock, and starvation.



Last Updated : 10 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads