Mutable vs Immutable Objects in Python
Adrian De La Asuncion Buelvas
FrontEnd Web Developer at Mercadolibre | React | Typescript | NodeJs | React Native
Everything in python is related to objects and almost everything has attributes and methods. an object has name value and type.
objects are related to object oriented programming. For this I should explain what a class is.
A class is a template where the objects will be created and an object is a set that belongs to the class.
- In the previous image the class would be Animal and the objects would be Dog and Sheep.
- And the attributes would be the color (yellow and white)
- The methods would be the actions (execute or write).
Now that you know this I am going to explain the ones that are mutable and immutable.
Id and Type
id() is a built-in Python function that only accepts parameters and returns the identity of an object.
type() it is a built-in calling method that returns the data type of a variable or if an object is passed as an argument it returns its type.
As we saw we can compare two objects to obtain the types and identifications to be able to change the variables of the object.
Immutable Object
Immutable objects are the easiest to use in programming (they are usually the simple data types: String, Integer, Boolean, str, tuple, frozen set.) because they do exactly what they are expected to do at any given time, and to work they are the most punishing to memory (they are not optimized to take up less memory when copying, and further degrade memory life by writing more times).
an example of immutable objects
my_var = "This is an Object"
When we declare a variable (my_variable) in Python (or any other programming language) a variable in memory address is created in memory. If we then assign to it (the variable is a cursor or pointer that “will point to the memory address of its value”) a mutable value, which for this example will be a text (String, in the example "This is an Object" ) and will be created in another memory location; then the above variable will point to the mutable object that contains its value.
In this case of immutability, and I think it's the most important thing because it tends to be the most confusing, it's when the immutable value (of a variable) is passed to a function as a parameter. When the immutable value is passed as a parameter to a function ("in the example, the var_function function variable creates a memory address), that value is immediately copied to another address in memory; therefore, you can deduce that if it is modified inside the function, it will need to be extracted (usually with the "return" inside the function) so you can use it outside the function, so what happens inside the function will stay inside the function.
Mutable Object
Mutable objects are the most "complex" to use in programming (they are usually data structures such as: dict, list, byte array, set) and not only because they are more complex because they are structures that have things, but they usually mess with the topic of pointers; and paradoxically they are the least damaging to memory (they are written only once and are always reused).
an example of mutable objects
my_var = ["This is", "an Object"]
In this example specifically a list of String values. The variable "my_variable" that points to another memory location where our Immutable object of type list will be stored in a memory space.
When the mutable value is passed as a parameter to a function (in the my_variable example), the variable will point to the mutable object, therefore it is NOT copied. Here you can already deduce some problems (when the object is modified inside the function it will be modified outside) and advantages (it does not take up more memory and the "copy" of a giant mutable is immediate).