Mutable, Immutable... everything is object!
So, I just recently woke up to the truth that in Python everything is an object, meaning the no discrimination of classes in this language, but... What is actually an object? Can they be changed? If so.. Which kind? In this blog I will approach the subject in the most possible simple terms hoping to bring the reader an insight while I increment my understanding by the doing of this blog, which demanded to research, think, rethink and and a brainstorm synergy achieved with my peers at some points of a whole day of studies.
In programming an Object is defined as abstract data type that can include different properties, methods, modules and other objects, created by the programmer. Python manage some flexibility by allowing the assignment of anything into a variable or passing arguments to a function.
Now we know that everything in Python is an object, we need to know what kind they are. A mutable object can be changed after it is created, and an immutable object can not. Objects of built-in types like (int, float, str, tuple, bool, unicode) are immutable and built-in types like (list, set, dict) are mutable.
How do we know if our Variable is a Mutable or Immutable Object?
As in Python everything is an Object, every variable holds an object instance. When we initiate an object, it is assigned to an object id. The type is defined at runtime, when it is set, it can never be changed,but its state can be changed if it is mutable. Every object has an identity, a type, and a value.
id() Function
This function can be seen as the unique and constant identity number of the object (an integer) while its life, that usually corresponds to the object’s location in memory.
In the example below, k, l and c are integer variables with a value, meaning that is a type int (integer) that comes from the class that it was instantiated at. The output is a unique number id, provided by the builtin function id():
type() Function
Is a built-in method in Python, used to determine the type of the variable used at runtime. If a single argument (object) is passed to type(), it returns the type of the given object. If three arguments are passed (name, bases, and dict), it returns to a new object type. In the example below each variable has a different type which is indicated after running function type:
Immutable Objects Example
These are simply objects that can’t change. Two types of immutable objects in Python are int, string, float, bool and tuple. In the example below we see a sting as an output, in this kind of data is not possible to change its value, what we can do is to assign a new string to that same variable.
Mutable Objects Example
They can be list, dict, set, byte array. Mutability is important for choosing data structures for a program because they are used to store chunks of data. So if you have a collection of 20.000 names that you want to safe, you can store them in an immutable tuple.In the simple example below the tuple t contains different data types elements, the first one is an immutable string and the second one is a mutable list.
tuple t = (‘friends’, [1, 2, 3, 4, 5, 6...])
How objects are passed to Functions
They are able to make these changes because they are being passed a reference of the object. In Python, that is the same as the object memory address. This is known as “call by sharing”. Immutable objects are also passed around by reference to them.
In the example below the same object is passed to the function, but the variables value stay the same even when the object is just as the previous one. The value is called by the function if the value of the variable is passed , not the object itelf, the variable referencing the object does not change but the object itself is being changed within the scope of the function.
def updateNumber(k): print(id(k)) k += 10c = 38 print(id(c)) # 10055680 updateNumber(c) # 10055680 print(c) # 38
In conclusion we know that Python treats everything as an object and that all objects have an id and type. An object value can be changed but not its id and type. When we pass by sharing that means that mutable objects are passed around in functions, so the caller and called functions can modify the object.
Immutable objects have no such methods, and therefore cannot be changed. When immutable objects are being passed around functions it is by reference to the object, so unless the return value is exactly the passed argument, it will return a new object.