Search Here

Mutable and Immutable Objects in Python

Hello guys how are you!!! and I hope you will be fine and also enjoying this tutorial. In my previous post you learned about  python objects, variables, type and id in detail. Today you will learn what are mutable and immutable objects in python? And what are difference between mutable and immutable objects.



Difference between mutable and immutable objects:

         Objects in Python may be mutable or immutable. Mutable objects may change value; immutable objects may not. Some immutable objects at times may look like they are changing their values. But they are not actually changing their values though. The distinction is visible using the id function.  Integers, container, objects and strings, things like these may appear to be changing values when they are not.
For example, here in the Python shell, we can create an object, and we will call it x, and we will assign a number to it, 20. Integers are immutable objects in Python, and if we look at the value, we see that it's 20.

 Mutable and Immutable Objects in Python

If we look at its type, we see that its type is int. If we look at the id, we see that it has this unique identifier.

 Mutable and Immutable Objects in Python


Now if I change the value of x, and remember it's an immutable object so I cannot change the value of an integer, but I assign x = 21, what I have actually done is that I have changed this variable x to refer to a different integer. Variables in Python are references to objects.
So this reference is now referring to a different object. If I type id of x, you will see I get a different identifier. If I type x, I see that it's 21. If I assign x back to 20 and take its id, you will see that this id is now exactly the same as the one up there. 


Mutable and Immutable Objects in Python


        So I did not actually change the immutable value. I simply changed the variable to refer to a different object.
This is an important distinction, and it's one that may take a while for you to get your head around, but we'll cover some of those as we get into this in more detail. There will be times when something that you want to do, like changes the value of a string or to insert objects in a tuple, you may find that you need to use a different type, a mutable type instead of an immutable type, in order to be able to accomplish where it is you are trying to accomplish.


In fact, most fundamental types in Python are immutable. Numbers, strings, and tuples are the fundamental types that are immutable. Mutable objects include lists, dictionaries and some other objects, depending upon their implementation. Today you learned the difference between mutable and immutable objects in python in very detail. You also learned that whats objects are mutable and what are not.  So as you create your code in Python, simply be aware that there are some types that are immutable - and these are typically the more fundamental types - and other types that are mutable . And as we get into the details of these types, we will talk about the things you can and cannot do with mutable and immutable objects.



Post a Comment

0 Comments