Difference between Mutable and Immutable Objects
Difference between Mutable and Immutable Objects
As we all know that Python is an object-oriented programming language. Object-oriented programming approach is based on the objects and the classes’ stores the data in the form of fields which is commonly known as attributes. In an Object-oriented programming language, the OOPs have two main keys without using them OOPs can't be achieved. Below are the main keys.
- Class
- Objects
And the main pillars of the Object-Oriented Programming language are:
- Polymorphism
- Encapsulation
- Abstraction
- Inheritance
What is class and object?
Class:
Class is mostly called the user-defined data type where user can define their own functions and methods. The class is also known as the blueprint of the object. which guides the object to the path and the function to be performed. Class doesn't define any data to the object. In Object-oriented programming, class plays a very important role that is why it is known as the basic unit of OOPs programming.
Object:
Whereas, the object is classified as a variable that is the instance of the class. The Object uses to store class functions and methods, and the data. that means the Object can store multiple data and data structures.
The objects are of two types that are mutable objects and Immutable objects
What is Mutable objects?
Mutable objects refer to those objects whose values or the content can be changed after initialization as per the requirement and usage. Mutable objects are classified as List, Set, Dictionary, and User-Defined Classes. For example, we declare a list with the values as "Red", "Green", and "Orange" and want to change the last value with "Blue" and First value with "Black". So, List, Set, and Dictionary allow to change the values. But the rest don't allow the same.
How to declare List, Set, Dictionary, and User-Defined Classes in Python?
In Python, Declaring the variable with a specific data type is not a big deal. Hence same goes with the List, Set, Dictionary, and user-defined data types. No heavy and fancy code require to declare all this let us see how to declare all of them with the Syntax of each:
#list # syntax for list List_name=["Value1","Value2"] #declaration of list List=["Orange","Apple","Mango"] #Set #syntax for set set_name = {"Value1","Value2"} #declaration of Set set= {"Orange","Apple","Mango"} #Dictionary #syntax for Dictionary Dict_name={ 'repres1':"Value1", 'repres2':"Value2"} #declaration of Dictionary Dict={'Orange':"Orange",'Red':"Apple",'Yellow':"Mango"} #user defined Class #syntax for user defined Class class class_name(): #class block #declaration of user defined Class class classes(): print("Hello World!")
What is Immutable objects?
As per the name, it's clear that it is the opposite of mutable objects. That means Immutable objects refer to those whose values or content cannot change after initialization as per the requirement of the user or programmer. In Python, Number, String, Tuples, Frozen Sets, Bytes, and User-Defined Classes are the types of Immutable objects.
Let us understand this with example, for example if we declare a tuple with some value and want to change it after the initialization. So it will show error to us.
How to declare Tuples in python?
#tuples #Syntax for tuples tuple_name=("values","values") #declaration of tuples tuple=("one","Two","Three")
Programs
Programs1: To check Immutable object(Tuples)
# Python code to test #immutable object tuple1 = ("Black", "Blue", "Orange", "Red") tuple1[0] = "Green" print(tuple1)
Output:
Traceback (most recent call last): File "c:\Users\Dodo\OneDrive\Desktop\desktop\sachin\JavaTpoint\dictionary\practical.py", line 4, in <module> tuple1[0] = "Green" TypeError: 'tuple' object does not support item assignment
Programs2: To check Immutable object(String)
# Python code to test that # strings are immutable message = "Welcome In JavaTpoint" message[0] = 'Hii' print(message)
Output:
Traceback (most recent call last): File "c:\Users\Dodo\OneDrive\Desktop\desktop\sachin\JavaTpoint\dictionary\practical.py", line 4, in <module> message[0] = 'Hii' TypeError: 'str' object does not support item assignment
Programs3: To check Mutable object(List)
# Python code to test that # lists are mutable color = ["Red", "Blue", "Green"] print(color) color[0] = "Black" color[-1] = "Orange" print(color)
Output:
['Red', 'Blue', 'Green'] ['Black', 'Blue', 'Orange']
Difference Between Mutable and Immutable Objects:
Basis | Mutable Objects | Immutable Objects |
Meaning | Mutable objects refer to those objects whose values or the content can be changed after initialization as per the requirement and usage | Immutable objects refer to those whose values or content cannot change after initialization as per the requirement of the user or programmer |
Type | All the classes and its classes are mutable by default by their nature. | String and all wrapper class are immutable by their nature. |
Final Class | To create a Mutable object, the class don’t require to be final | To create a Immutable object, the class require to be final |
Examples | List, Set, Dictionary, and User-Defined Classes | Number, String, Tuples, Frozen Sets, Bytes, and User-Defined Classes |