Python: Checking that everything is object! Mutable, Immutable objects.
We know that everything in python is a “class”. Every variable holds an instance of an object. There are two types of objects in python; Mutable and Immutable objects. When an object is instantiated, it is assigned a unique object id.
“ The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object; in other hand, immutable objects can’t change their state or content.”
What is “id” and “type” functions?
Python have a built-in function called as “type”, which returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.
If single argument “type(obj)” is passed, it returns the type of given object. i.e.
Another example:
On the other hand we have, built-in function called as “id”. This function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes and immutable type may have the same id() value, like top image in this blog. ie.
After execution of these assignment statements, we know that a and b will refer to a string with the letters “holberton”, and thus, both refers to the same object.
Since strings are immutable, Python optimizes resources by making two names that refer to the same string value refer to the same object.
This is not the case with lists or another mutable object:
For this case, representation will be with two different objects, i.e.
a → [4, 5, 6]
b → [4, 5, 6]
a and b have the same value but do not refer to the same object.
Aliassing
Since variables refer to objects, if we assign one variable to another, both variables refer to the same object, ie.
The same list has two different names, a and b, we say that it is aliased. Changes made with one alias affect the other.
NOTE: In general, it is safer to avoid aliasing when you are working with mutable objects. Of course, for immutable objects, there’s no problem.
Mutable objects
Mutable objects can change their state or contents. These are of type list, dict, set . Custom classes are generally mutable. i.e with a type List
Image above tell us, that accesing to index of the list, we can change its value at runtime whenever we want.
Immutable objects
Immutable objects can’t change their state or content. These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created. i.e with a type of tuple.
Let’s test with another immutable object type.
If we want to add to the string a new line after “hello” word, python raise an TypeError exception where object does not support this assignment because after being instantiated, the object stay like in “read only ” mode, with
no possibility of being modified.
NOTE: There is an exception in immutability as well. We know that tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects. ie.
tup = ([9, 8, 7], 'Julian Villegas')
The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.
Why does it matter and how differently does Python treat mutable and immutable objects?
Mutable and immutable objects are handled differently in python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy. Whereas mutable objects are easy to change. Use of mutable objects is recommended when there is a need to change the size or content of the object.
How arguments are passed to functions and what does that imply for mutable and immutable objects?
The most common evaluation strategy when passing arguments to a function has been call by value and call by reference.
Call by Value
In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller’s scope will be unchanged when the function returns.
If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can’t be changed within the function
Call by Reference
A function gets an implicit reference to the argument, rather than a copy of its value. As a consequence, the function can modify the argument, i.e. the value of the variable in the caller’s scope can be changed.
If we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place