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, it’s 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.
- Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created.
# Python code to test that# tuples are immutabletuple1=(0,1,2,3)tuple1[0]=4print(tuple1)chevron_rightfilter_noneError :
Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment# Python code to test that# strings are immutablemessage="Welcome to GeeksforGeeks"message[0]='p'print(message)chevron_rightfilter_noneError :
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 : These are of type list, dict, set . Custom classes are generally mutable.
# Python code to test that# lists are mutablecolor=["red","blue","green"]print(color)color[0]="pink"color[-1]="orange"print(color)chevron_rightfilter_noneOutput:
['red', 'blue', 'green'] ['pink', 'blue', 'orange']
Conclusion
- 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. - 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 tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects.
Consider a tupletup = ([3, 4, 5], 'myname')
The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.
As a rule of thumb, Generally Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.
Recommended Posts:
- Reading Python File-Like Objects from C | Python
- Barrier Objects in Python
- Python Classes and Objects
- File Objects in Python
- Timer Objects in Python
- Python | Counter Objects | elements()
- Byte Objects vs String in Python
- Python | Unit Test Objects Patching | Set-2
- Python | Unit Test Objects Patching | Set-1
- Python Tkinter | Moving objects using Canvas.move() method
- Python | Draw rectangular shape and extract objects using OpenCV
- Count number of a class objects created in Java
- Django Request and Response cycle - HttpRequest and HttpResponse Objects
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ShreyanChowdhury



