Mutable, immutable… everything is an object

Brayan Florez
4 min readJan 16, 2020

As the title says, everything in Python is an object and we must understand that those objects can be either mutable or immutable. To understand this let’s see the following chart in which we can see what type of variables are mutable or not.

Id() and type() functions

The id(object) built-in function returns the identity of an object which is an integer that guarantees to be unique and constant for this object.

>>> A = 10
>>> B = 10
>>> id(A)
10105376
>>> id(B)
10105376
>>> A is B # Check whether two names refer to the same object
True

In the previous example, we could see that as both variables have the same value, Python optimizes resources by making two values that refer to the same int value refer to the same object.

The type(object) returns the type of an object

>>> A = 10
>>> type(A)
<class 'int'>

Mutable vs immutable

To make it clear let’s explain this in the easiest way possible, a mutable object can be changed after it’s created and an immutable is totally on the other side (meaning that it doesn’t allow to change its value after its creation)

Let’s see the following example to understand this in a better way

In the previous example, we can see the following outputs in which A is an immutable object it keeps its value forever

>>> A = 'Holberton'
>>> B = A #B now has the value that A has
>>> A #Print A
'Holberton' #Output A
>>> B #Print B
'Holberton' #Output B
>>> B = 'Betty' #Here we change the value of B
>>> B #Print B
'Betty' #B has acquired another value
>>> A #Print A
'Holberon' #Here we can see that A has still the same value

If both variables are mutable, they both change when any of them is modified in-place (see the following example)

>>> A = [1, 2, 3]
>>> B = A
>>> A.append(4) #Value of A is changed by adding to the list 4
>>> B #Print B
[1, 2, 3, 4] #It is modified since B acquired A value
>>> A #Print A
[1, 2, 3, 4]

Why does it matter to treat mutable and immutable objects?

It really matters because knowing how to treat them you can’t avoid unexpected behavior.

How differently does Python treat mutable and immutable objects?

A mutable object is not that efficient in terms of memory and time considering that Python will reassign all the elements (for example in a list). On the other hand, immutable objects can be faster and less heavy talking about memory since the whole object is not reassigned every time we modify it.

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

Here we must talk about of pass by value and pass by reference

When we pass by value, the object value does not change. But why does this happen? Well, when the value it’s called by the function only the value it’s passed through it, not the object itself.

Take a look at the following example

def increment(n):
n += 1

a = 10
increment(a)
print(a) #10

And when we pass by reference, a mutable object it’s passed through the function and at the end its value it’s changed

def increment(l2):
l2 += [3]

l1 = [1, 2]
increment(l1)
print(l1) #[1, 2, 3]

There is a bit exception…

As we have seen during the post, tuples are immutable objects meaning that its value can’t be changed after creating it. But wait, what if a tuple has a list as one of its attributes? Well to explain that look at the next example

tuple_ex = ([1, 2, 3], "Hello")

the tuple itself contains both types of objects, immutable and mutable and by saying that we already know that we are able to change its list attribute “[1, 2, 3]” as lists have mutable methods

That’s all for this blog. Thanks for reading it!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response