Mutable, Immutable... everything is an object!
Xavier E. Figueroa Muniz
Software Engineer at Lockheed Martin | Bachelor's in Computer Sciences | Holberton School
When it comes to the Python Programming Language one of the factors to take into consideration is its use of Object Oriented Programming (OOP). By understanding this concept and how it works through encapsulation or forming of a collection of things, we can understand how these "things" can be considered properties of an object. Now in python an object belongs to a class. A class contains or collects all types of information about certain information, which could be considered properties of a class, and works like its a blueprint structure of that class. Now if we were to have custom information for just an instance of that class and not permanently then it is possible through objects. In short: An object is an instance of a class.
Following this concept we can start to visualize the fact that in Python data types can be considered objects.
Id and Type
To better understand this concept we can take into account the Id and type of an object. These are factors that help us differentiate between one and another. Using Id we can differentiate and understand better whether an object is mutable. Lets take this example for instance:
>>> foo = [1, 2, 3] >>> foo2 = foo >>> id(foo) 139802015065736 >>> id(foo2)
139802015065736
Here we can see how the id for one variable stays the same as the other due to the fact that we are working with a mutable object. Because of this we must be careful when working with this object since it will named under two variables.
Mutable Objects
Mutable objects are class instances whose values can be changed without having to change their identity or id in memory.
Some mutable objects include:
- lists
- sets
- dictionaries
Immutable Objects
Immutable Objects, in contrast to mutable objects, cannot have their value changed. In the case that we were to try to change the value by assigning it something different it would also cause its id to change since it would be considered a new object.
>>> var = "hi" >>> type(var) <class 'str'> >>> id(var) 139802044784800 >>> var = "hey" >>> type(var) <class 'str'> >>> id(var) 139802015447168
Here, in the previous example, we can see how by changing the value of the variable from "hi" to "hey" changes the id of said variable since it is now considered a different object of the same (immutable) type str.
Some Immutable Objects include:
- int
- str
- float
- tuple
- frozenset
Why does it matter and how differently does Python treat mutable and immutable objects?
It is of importance to understand this since the use of a mutable or immutable object can affect greatly when it comes to using Python. When it comes to mutable objects Python can continue using the same object since it has the same unchanged id. On the other hand, when using an immutable object and a change where to be made, Python would be generating a new object with new id.
How arguments are passed to functions and what does that imply for mutable and immutable objects?
Arguments in Python, or parameters, are taken into a function as a reference to an object. This would indicate that if we were to take an argument into a function the fact that is mutable or immutable can affect greatly. If we take for example a mutable object as an argument passed into a function, since the value of the mutable object can be changed freely, without changing the id of that object, we can obtain as a result the new changed value from said function.
In contrast, if we take an immutable object and pass it to a function then if the value were to be changed then the id of the object would be changed as well resulting in a new object. Now if we were to verify the value of that argument outside of the function it would maintain the same original id since it was not altered. Whilst the altered value caused by the function is stored in a new object with a new id.