Mutable And Immutable Objects In Python
Mutable And Immutable Objects In Python
Every variable we define or declare in python are objects. These objects can be mutable or immutable. what does it mean?
We will dive deep into the details of it but first let’s take a short introduction to python language.
Since, we know that python is a typeless high-level programming language. It is used in machine learning, Artificial Intelligence, Web Development, as well as in all cutting Edge Technologies in software industries now-a-days.
Python allows both Object Oriented programming and Procedural Programming and as we all know that it is a general purpose programming language.
The biggest strength of python is the huge collection of the Standard libraries which can be used in:
- Machine Learning
- GUI Applications
- Web Framework
- Image Processing
- Web Scraping
- Test Framework
- Scientific Computing
- Artificial Intelligence
- Data Visualization
Since we are here to know about the mutable and immutable objects in the python. In python every variable holds an instance of objects. There are two types of objects in python i.e mutable and immutable objects.
The difference between the mutable and immutable object is that immutable objects can’t be changed after it is initiated whereas mutables can be changed there state in the program. Let us understand this more clearly -
Python Immutable Objects
The immutable objects are those objects that can never change their value in place. If you are assigning a value to the variable for example : x=65
As we can see the above diagram, x variable is pointing to a memory block whose address is 10203410. Now , if you change the value for x, then the value never changes in place ( x assigning to the previous address). It stores the value in a new memory block and now x variable starts pointing to it. Let’s understand this through a program -
The output of the following program -
There are some built-in objects in the python which are immutable in nature.
- Numbers - int, float, complex
Some immutable objects use data pooling, It means they are sharable.
Sharable - Python Immutable Objects
Some python immutable objects are sharable, it means the number of reference variables or objects can point to the same memory in case they have the same value.
Let us understand this through a program -
In this case, x, y and z have the same value then they will share a single stored value instead of storing multiple times.
So, internally all three x, y and z will point to the same memory block and this called memory pooling.
If we print the address of x, y and z then, all three will be the same.
The output of the following program :
Python Shared Objects - Memory Pool
|int||-5 to 256|
|str||0 to 256|
|tuple||() empty tuple|
Python Mutable Objects
Python mutable objects are those values that can change in place. Since, there are also some built-in objects in python which are mutable in nature and those are -
We can change it in runtime after once it has been created. Let us understand this through a program -
The output of the following program will be -
As we discuss here the mutable and immutable objects in python so it is necessary to also discuss the immutability and mutability of the functions. As we know that mutable object is called by reference in the function, as it can change the original value of the variable itself. Therefore, it is necessary to copy the value into another variable. Immutable object can be called by reference so that it’s value cannot be changed anyways.
Some important Points to remember about python objects :
- Mutable and Immutable objects are handled differently in python. Immutable objects are quicker to access and it is very much expensive to change it because immutability involves the creation of a copy in the python.
- Whereas the mutable object is very much easy to change in this type programming of language.
- The use of mutable objects in the python is recommended when there is a need to change the size or content of the objects.
Exception in Immutable objects
However, there is also an exception in the immutability and mutability of the object in the python as well. As we know that the tuple in the python is an immutable object. But the tuple consists of the sequence of the names which is unchangeable binding with the objects.
Consider a tuple:
As we know that tuple consists of string and the list both. Strings are immutable so we can’t change its value. But the content of the list can change.
The tuple itself is not mutable but it contains items that are mutable in nature.
Since, as we know that as a rule of the thumb, generally primitive-like types are immutable in nature whereas, customized-like types are mostly mutable in nature.