Difference Between Mutable and Immutable in Python
Apart from being different based on the susceptibility to the changes, there exists other difference between mutable and immutable objects in Python which are explained in the table provided below:
Key Differences Between Mutable and Immutable in Python
Mutable | Immutable |
The objects can be modified after the creation as well. | Objects can not be modified after the creation of the objects. |
Classes that are mutable are not considered final. | Classes that are immutable are considered final. |
Thread unsafe. | Thread-safe. |
Classes are not made final for the mutable objects. | Classes are made final for the immutable objects. |
Example: Lists, Dicts, Sets, User-Defined Classes, Dictionaries, etc. | Example: int, float, bool, string, Unicode, tuple, Numbers, etc. |
Formulas for GATE Computer Science Engineering - Digital Logic
What is Mutable in Python?
In Python programming language, whenever an object is susceptible to internal change or has the ability to change its values is known as a mutable object. Mutable objects in Python are generally changed after creation.
For mutable objects in Python, their values can be changed but the identity of the object remains intact. The objects in Python which are mutable are provided below:
- Lists
- Dicts
- Sets
- User-Defined Classes
- Dictionaries
Formulas for GATE Computer Science Engineering - Computer Organization & Architecture
What is Immutable in Python?
Similarly, when the objects in the Python are not susceptible to the internal change are known as the immutable objects. They can not be modified once they are created. For example, int in Python is immutable hence they can not be modified once they are created.
Objects in Python which are immutable are shown below:
- int,
- float,
- bool,
- string,
- Unicode,
- tuple
- Numbers
- Frozen Sets
- User-Defined Classes
Check out some important topics related to the difference between mutable and immutable objects in Python in the table provided below:
Comments
write a comment