Yoyman Manuel Castellar Miranda
3 min readMay 27, 2020

--

Python3: Mutable, Immutable… everything is object

Introduction

Today I would like to share a topic that I learned today at Holberton School. Today's project dealt with objects in python. With the help of my peers, we set out to solve certain questions, such as. What is an object? What is the difference between a class and an object or an instance? What is the difference between a mutable and immutable object, and much more. I would like in this blog to be able to share another questions below. Let's go.

id and type

The id () function is a built-in python function that receives an object as an argument, which in turn returns another object that serves as a unique identifier for that object. According to the official python implementation, Cpython the type of value that is returned is the memory address where that object is stored.

>>> s = "Hello World"
>>> pi = 3.14
>>> id(s)
11555888
>>> id(pi)
47907328

The type function is also incorporated in python and like the id () function it also receives an object as a parameter and as its name indicates it returns the data type that this object has.

>>> a = 'Hello World'
>>> print(type(a))
<class 'str'>

Mutable objects

A mutable object refers to the fact that variables can change, modify or update their value, among the most common are lists, dictionaries, etc.

>>> lis = [1, 2, 3]
>>> id(lis)
139751090351168
>>> lis += [4]
>>> id(list)
139751090351168
>>> print(lis)
[1, 2, 3, 4

Inmutable objects

An immutable object are those objects whose value cannot be changed, modified or updated. however, as any variable, a new value can be assigned. Among these we find objects of type int, long, float, bool, string, tuple.

>>> string = "Holberton"
>>> print(string)
Holberton
>>> id(string)
139751072383728
>>> string += "School"
>>> print(string)
HolbertonSchool
>>> id(string)
139751072392688

As we have just seen in the previous examples. A mutable object when updating its value, its id was the same. but an immutable object like its value cannot be modified, and even if the same variable is used, to change its value, it creates a new object with the same name and is stored in a new memory address.

why does it matter and how differently does Python treat mutable and immutable objects?

It is important because knowing this will help us to have better control of the data and the functions we use, in such a way to develop a program with better performance, taking advantage of the memory spaces as best as possible. and avoid future errors in the program.

How arguments are passed to functions and what does that imply for mutable and immutable objects?

When an object is passed as an argument to a function, a new reference is created and not a copy, this can apply to both mutable and immutable objects.

def new_f(newobj):
print("Inside: ", newobj)
print(id(newobj))
a = 10
print("outside: ", a)
print(id(a))
new_f(a)

OUTPUT:

outside:  1010105376Inside:  1010105376

As we saw in the previous example, an object called “a” is created and we assign an integer of value 10, and additionally we create a function which receives the object “a” as an argument. we can see that both inside and outside the function we have the same value and the same memory address, because the argument takes the object as a reference and does not change

But if we make a change within the function …

def new_f(newobj):
newobj += 5
print("Inside: ", newobj)
print(id(newobj))
a = 10
print("outside: ", a)
print(id(a))
new_f(a)

OUTPUT:

outside:  1010105376Inside:  1510105536

We realize that despite having the same reference, once the value is modifying, and being int a non-mutable object, within the function a new object is created with a new heat and a new memory address.

Thank you!.

--

--