Objects in Python — Mutable vs Immutable
In this blog post I’ll be discussing these concepts of the Python programming language:
•id() and type()
•Mutable objects vs immutable objects
•How arguments are passed to functions and what does that imply for mutable and immutable objects
id() and type()
id() is a built-in function in Python. The function takes one parameter, and returns the identity an object. In C terms, it returns the address in memory. An address in memory for C, is an unique id in Python. Two objects can share the same id() value. Below is an example:
As you can see a and b both have equal value, 10. When passed as parameters to the id() function, both return the same id value. However, notice that when we change the value of the variable b, the return from id() changes.
type() is a Python method that takes one parameter and returns the type of the object passed as a parameter. Usually used for debugging purposes to validate input. Below is a demonstration:
type() can also take three parameters: type(name, bases, dict). When given three parameters, it can be used to dynamically initialize classes with attributes.
Mutable objects vs immutable objects
All objects in Python can be either mutable or immutable. Simply put, a mutable object can be changed after creation, an immutable object cannot be changed after creation. Below is a list of data types, along with their description and whether or not they are immutable:
You can use to python interpreter to see whether an object is immutable or not.
Here, you can see the value of 10 is assigned to the variable num1. The id() function returns the same id for the number 10 and the variable num1. But once the value of num1 is altered, id() returns a different value. Object 10 was never modified, it still returns the same id() value, only the object which num1 was pointing to changed.
Here is an example if an mutable object. A list is created and a second one pointing to the same object is created as well. The pop() function is then used to modify the list. As you can see, after the list is modified, the object id still remains the same.
Immutable objects are good for when you need to ensure that the object you created will not change. Mutable objects are good for when the object might need to be changed.
How arguments are passed to functions and what does that imply for mutable and immutable objects
For the sake of memory efficiency, is is important to pass the proper objects to functions. Since they are treated differently when passed to functions.
If a mutable object is called by reference in a function, it could change the original object. To prevent this from happening, a copy of the original value needs to be made. However, immutable objects can be called by reference since their values cannot be changed.
Since the list was called via call by reference, the changes where made to the original list itself.
In the picture above the same object is passed to the function, but the object’s value doesn’t change. This is called pass by value. When the value is called by the function, only the value of the variable is passed, not the actual object. So the original variable referencing the object is not changed. Only the object itself, but within the function scope only. Resulting in id() returning the same value for both calls above, since the original object was never modified.
You can read more about OOP in Python here: https://www.python-course.eu/python3_object_oriented_programming.php