Python Immutability

Immutability: Everything in python is an object, once we create an object we cannot perform any changes in that object and if we are trying to implement any changes, then with those changes a new object will be created this process is called as immutability or Non-changeable behavior of an object is called as immutability.

Example: int,float,bool,string,tuple

Let us consider an example where x=10, everything in python is an object, so here 10 is an object and x is the reference variable of that object, and for every object, the address will be there, so to print that address of an object we need id() function, print(id(x))

x=10
print(id(x))

printing-id-of-x

If I change the content of the x as x=x+1, that x is an object when we are trying to make changes to the object then a new object will be created, and hence x will be pointing to that new object, so if you print(id(x)) then the address of the new object is going print

And ten will not have any reference as x pointing to the new object so that it will be moved to the garbage collector

x=x+1
print(id(x))

new-object-has-created

Let us consider another one example, x=10, and y=x so here x and y both are pointing to 10 only, you can check this practically

x=10
y=x
print(id(x))=?
print(id(y))=?

x-and-y-pointing-to-same-address

If I change the content of y, y=y+1 and then a new object will be created and hence now onwards y is pointing to the new address of the new object.

x=10
y=y+1
print(x)
print(y)
print(id(x))
print(id(y))

x-and-y-pointing-to-different-address

History of Python

Use of Immutability

Let us see why we need immutability by using the following example

If a=10, b=10, and c=10, here three reference variable are going to point to the same object because in python if we are trying to create an object with the same content again, it will check whether that object is already present or not if it is already present then it is going to use the same object again and hence a,b,c three variables are going to point to the same object, this process is called object reusability.

To check this practice, we can print the id of all the three reference variables

a=10
b=10
c=10
print(id(a))
print(id(b))
print(id(c))

a-b-c-pointing-to-same-object

  • As it is reusing the object and hence memory utilization will be improved
  • Creating an object in any programming language is a costly operation, but python supports reusability of an object, and hence performance will be improved
  • And the same method will be applicable for Float data types also, consider a=10.234 and b=10.234 so here only one object has been created, and both a and b are pointing to the same object.
  • To check whether both objects are pointing to the same object or not, we have an operator called is operator, if it returns True then both variables are pointing to the same object or else False
    a=10.234
    b=10.234
    print(a is b)
  • When we are trying to execute this code in the console, we will get False
    executing-through-console
    But the same thing when we are trying to achieve by creating a separate file and run this code through command prompt then it will return as True, that means the IDLE is not a standard one to execute the python programs, this one is useful only to perform the small code snippets so no need to worry about this, please go through below images
    stored-in-a-file
    executing-through-command-prompt
  • Even for the boolean case, the above method is applicable
    a=True
    b=True
    print(a is b)
    a=False
    b=False
    print(a is b)
    
    ​

    for-boolean-values

  • Even in string data types, the same concept will be applied
    a="dhan"
    b="dhan"
    print(a is b)
    ​

    string-form

Note: the Object reusability concept is not relevant in the complex data type
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions