Mutable objects in Python enable the programmers to have objects that can change their values. They generally are utilized to store a collection of data. It can be regarded as something that has mutated, and the internal state applicable within an object has changed. In this Python tutorial, you will learn:
What is a Mutable Object? What are Immutable objects? In Python, everything is an object Mutable objects in Python Immutable objects in Python Implications for dictionary keys in Python Exceptions in immutability Mutable vs. Immutable objects Python Immutable Data Types
What are Immutable objects?
Immutable objects in Python can be defined as objects that do not change their values and attributes over time. These objects become permanent once created and initialized, and they form a critical part of data structures used in Python. Python is used in numbers, tuples, strings, frozen sets, and user-defined classes with some exceptions. They cannot change, and their values and it remains permanent once they are initialized and hence called immutable.
In Python, everything is an object
In the Python programming language, everything can be regarded as an object comprising lists, integers, and functions. This feature can be compared with other programming languages which support objects. This feature can be verified using a Python interpreter as shown below: – Python code: Output: Further, Python provides a built-in function named id that returns the object’s address as present in the memory of the Python programming language. Python code: Output: In the above code, the id function having syntax as id(obj) gives the address of obj in Python memory. Here, there is an object named z, and it has an assignment of 200. The object z is then passed into id function as id(z), and the Python delivers the object’s address as 9795360.
Mutable objects in Python
In a mutable object, the object’s value changes over a period of time. In this example, we have explained mutable objects in Python, and this utilizes lists as an application of mutable objects as shown below: – Python Code: Output: As we can see in the above-given example, the mutable list in Python had values of 1,2,3. The first element of the mutable list is changed from 1 to Guru99, and it does not create a new object when a new value is initialized. Here we can use the id method to utilize it. Following illustrates the use of the id method for mutable objects as shown below: – Python Code: Output The following figure illustrates the mutable object in Python as shown below: –
Immutable objects in Python
Immutable objects in Python are objects wherein the instances do not change over the period. Immutable instances of a specific type, once created, do not change, and this can be verified using the id method of Python. Let us take an example of integer type objects in Python that illustrates the concept of immutable objects in Python as shown below: – Python Code: Output It could be seen above that there is change in “a.” Let’s study how the mechanism works:
There is no change in the object’s value when the initialization of “a” with 344. Instead, a new object is created and is bounded with “a.” The other object assigned as 244 would no longer be accessible. The above example utilized an integer object.
At a=244, a new object is created and referenced to “a” as shown below: –
Post using a=344, there is a new object referenced with “a”. The following diagram represents the same: –
Therefore, whenever there is the assignment of a new value to the name of int type, there is a change in the binding of the name with another object. The same principle aligns with tuples, strings, float, and Boolean hence termed immutable.
Implications for dictionary keys in Python
Dictionaries can be defined as the ordered collections that stores data in the key format and does not allow duplicates. Dictionaries contains one key which have corresponding value pair aligned to it. They are mutable in types, and their content can be changed even after their initialization and creation. At any moment, the key points to one specific element at a time. The keys of dictionaries are immutable. Let us take a hypothetical scenario as shown below: – Output: – The above Python code does not yield any output, and instead, it generates a type error of unhashable type. This is a hypothetical situation and is not used in the Python compiler. Here, a is defined as [4,6], and in the dictionary, it is defined as x. Here, b is defined as [5,6,7], and in the dictionary, it is defined as y.
The key ‘a’ has the value of [4,6], and it is further initialized to x. The key ‘b’ has the value of [5,6,7] which is further initialized to ‘y’ in a dictionary. Now assume that the value of ‘a’ is appended with 5 and 7, which is a key for the dictionary. Then the dictionary has been mutated, and it would give both ‘x’ and ‘y’ as values for the above dictionary.
Consider the following scenario as illustrated above: – Hence, as a programming language, Python makes keys of the dictionary immutable, and dictionaries are immutable data types.
Exceptions in immutability
However, Python provides exceptions to immutability such exceptions can be observed for the tuple object type. A tuple can be a combination of mutable and immutable object types. Let us take an example to explain the exceptions in immutability as shown below: – Python Code: Output: You can see in the above code, that the first element, which is a list, is mutable, whereas the tuple is immutable. The value of the tuple cannot be changed, but the contents of the list present inside the tuple can change its value. Therefore, this raises an exception that the immutable objects do not change their value, but the value of constituents changes their value.
Mutable vs. Immutable objects
Here are major differences between Mutable and Immutable Objects: