Mutable vs Immutable Objects in Python
Every variable in Python holds an instance of an object. There are two types of objects in Python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, its state can be changed if it is a mutable object. To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.
Mutable and Immutable Objects in Python
Let us see what are Python’s Mutable vs Immutable Types in Python.
Immutable Objects in Python
Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode, and tuple. In simple words, an immutable object can’t be changed after it is created.
Example 1: In this example, we will take a tuple and try to modify its value at a particular index and print it. As a tuple is an immutable object, it will throw an error when we try to modify it.
Python3
# Python code to test that # tuples are immutable tuple1 = ( 0 , 1 , 2 , 3 ) tuple1[ 0 ] = 4 print (tuple1) |
Error:
Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment
Example 2: In this example, we will take a Python string and try to modify its value. Similar to the tuple, strings are immutable in nature and will throw an error.
Python3
# Python code to test that # strings are immutable message = "Welcome to GeeksforGeeks" message[ 0 ] = 'p' print (message) |
Error:
Traceback (most recent call last): File "/home/ff856d3c5411909530c4d328eeca165b.py", line 3, in message[0] = 'p' TypeError: 'str' object does not support item assignment
Mutable Objects in Python
Mutable Objects are of type list, dict, or set. Custom classes are generally mutable.
Example: In this example, we will take a Python List object and try to modify its value using the index. A list in Python is mutable, that is, it allows us to change its value once it is created. So once the color list is created with defined colors, we then changed the element on index 0 and the one on the last index from red and green to pink and orange respectively.
Python3
# Python code to test that # lists are mutable color = [ "red" , "blue" , "green" ] print (color) color[ 0 ] = "pink" color[ - 1 ] = "orange" print (color) |
Output:
['red', 'blue', 'green'] ['pink', 'blue', 'orange']
Python’s Mutable vs Immutable
- Mutable and immutable objects are handled differently in Python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy. Whereas mutable objects are easy to change.
- The use of mutable objects is recommended when there is a need to change the size or content of the object.
- Exception: However, there is an exception in immutability as well. We know that a tuple in Python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects. Consider a tuple
tup = ([3, 4, 5], 'myname')
The tuple consists of a string and a list. Strings are immutable so we can’t change their value. But the contents of the list can change. The tuple itself isn’t mutable but contains items that are mutable.
As a rule of thumb, generally, Primitive-like types are probably immutable, and Customized Container-like types are mostly mutable.
Please Login to comment...