Mutable And Immutable… Everything Is Object!

Joel Ramirez
5 min readMay 22, 2021

--

INTRODUCTION

In object-oriented programming, there is a very special feature, this is inheritance and it can be applied to all classes. and it is thanks to this feature that there are languages such as java and python, for the particular case of python, it was designed in such a way that the smallest value that a variable can take will always be an object.

This means that there is a superclass from which all data types inherit their methods and attributes, from simple data types such as integers or characters chains to user-created data types. this superclass in python is known as Pyobject

Types and Ids

The id is a unique and constant identifier assigned to a instaced variable and its value, this id is an object and its value can be accessed using the id() function. We can make an approximation between id and memory address

Source: Cafepy

The type of a variable refers to which class the instance belongs to, remember that almost everything in python can be considered objects, and for this reason we can use the type () function to know the class of the variable

The id and the type are objects defined during the creation of a variable and once created they will be unchangeable

s1 = "Hello"
s2 = "World"
print(id(s1)) #140626500175984
print(id(s2)) #140626500176176
print(type(s1)) #<class 'str'>
print(type(s2)) #<class 'str'>
s2 = "Hello"
print(id(s2)) #140626500175984

Appreciate how the assignment of different values to a variable generates a different id for each case. However, python assigns the same id to variables whose value is the same regardless of whether they are different variables. This applies to objects belonging to numbers (int, float), bools, strings and tuples. which are part of the immutable objects

Mutable objects

In this category we can find the lists, dictionaries, sets and classes of the programmer. An immutable object is known as objects whose values can change without affecting their id.

l1 = [1, 2, 3 , 4]
print(l1, id(l1)) #[1, 2, 3, 4] 140028585700864
l1[0] = (5)
l1.reverse()
print(l1, id(l1)) #[5, 3, 2, 1] 140028585700864

This is because to modify the value of a mutable variable a reassignment is not necessary, as its name indicates, the value of the variable can change, and for this we can use the methods of the class to which it belongs

Immutable objects

In the case of immutable objects it is more complicated because its value cannot change once defined, this means that to modify its value a reassignment is necessary (create a new id for a new value to the same variable)

a = 1
b = 2
print(id(a), a) #9784896 1
print(id(b), b) #9784928 2
a = a + b #or a += b
print(id(a), a) #9784960 3
print(id(b), b) #9784928 2

In this example we can see the creation of three values (1, 2, 3) with their respective aliases (a, b, c), when a = a + b is executed, the original value of a cannot change because this is immutable therefore a new value must be created and must have aliases ‘a’. Another way to verify immutable objects is through tuples because, since they are immutable, any attempt to modify their values will result in an exception.

a = (1, 2)
print(id(a), a) #140343551428032 (1, 2)
a[0] = 3 #TypeError: 'tuple' object does not support item assignment

Python Object Handles

Since everything in Python is an object, the main difference between these is the ability to modify or not its values and attributes. the mutability of an object is not the same for all. Immutable objects are faster to access and costly to change because they involve creating a copy. Whereas mutable objects are easy to change.

l1 = [1, 2, 3 , 4]
l2 = [1, 2, 3 , 4]
s1 = "joe"
s2 = "joe"
print(l1, id(l1)) #[1, 2, 3, 4] 139837454892544
print(l2, id(l2)) #[1, 2, 3, 4] 139837454260352
print(s1, id(s1)) #joe 140245909489648
print(s2, id(s2)) #joe 140245909489648
l1[3] = 5
l1.reverse()
print(l1, id(l1)) #[5, 3, 2, 1] 139837454892544
print(l2, id(l2)) #[1, 2, 3, 4] 139837454260352

In this example, two immutable objects (list) and two immutable objects (str) are defined. In the case of strings, you can see that python creates the object and its id, all variables that have the same value will have the same id. for mutable objects python assigns an id for each instantiation of the class, regardless of whether their values are the same

Arguments Passed To Functions

It is important to know the mutable or immutable state of an object to know how it will be handled within a function.

For immutable objects, the pass by value is used by functions to access the object’s value, and since it cannot change, the changes made within the function do not matter; outside the context of the function the object will continue to maintain its original value

def mod(a)
a = 2
a = 0
print(a) #0
mod(a)
print(a) # 0

For mutable objects, pass-by-reference is used by functions to access all the characteristics of the object. This makes it easy for functions to modify the object they are working with, so all changes made to the context will remain.

def mod(l)
l.append(5)
l = [1, 2, 3, 4]
print(l) #[1, 2, 3, 4]
mod(l)
print(l) #[1, 2, 3, 4, 5]

Finally, remember that everything is an object in python and its mutability which indicates how it will be treated by python

--

--