Yosri Bouabid
3 min readMay 26, 2020

--

Python3: Mutable, Immutable… everything is object!

Introduction:

As we learned in c, there is many types of variables we can use (int, float, char, …)

So does python, but the types in python are a little bit different, because everything is a Class. This way it make every type equal wither it was integer, list, dictionary, … so that it can manipulate it the same way. And this it has the power to save the most memory possible and make it the most efficient programing language today.

Id and type:

If we created a variable let’s say “a” and give it a value of 45:

>>> a = 45

We have just created a variable witch have an id and a type,so what are there usage.

basically id mean the address in the memory of this variable:

>>> id(a)

10915904

And type is the class of “a”:

>>> type(a)

<class ‘int’>

This two functions give us more information about the variables we use.

mutable objects:

Mutable are the object that can be changed, whether it was adding or removing an element. It can be list, set or dictionary.

For example list are a mutable object witch mean we can add to it :

>>> a = [1, 2, 3,4]

>>>a.append(5)

>>>a

[1, 2, 3, 4, 5]

We can also remove any element:

>>> a.remove(3)

>>> a

[1, 2, 4, 5]

We can search for an element by it’s index:

>>> a[0]

1

>>> a[-1]

5

immutable objects:

Immutable objects are objects that cannot be changed. Such as int, float, strings and more.

we can’t change an element inside a string nor removing.

>>> c = “Hello”

>>> c[0] = “h”

Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘str’ object does not support item assignment

But we can add two strings with the “+” sign:

>>> d = “ World”

>>> c + d

Hello World

And retrieve a specific element:

>>> c[0]

‘H’

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

As we passed in C, when we assign the variable a with a value of 5 we have to :

int a = 5

which mean that we located a 4 bytes in the memory specifically for this variable a, and give it value of 5.

But in python because everything is an object the value 5 is an object in it self

which mean when we add to it a value of 2, it’s like we just created a whole new variable.

>>> a = 5

>>> id(a)

10914624

>>> a = a +1

>>> a

6

>>> id(a)

10914656

in this case both the variable a and value “6” have the same id:

>>> id(a)

10914656

>>> id(6)

10914656

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

The functions in python take the arguments by it’s reference, which mean it has a full control over this variable:

>>> l = [1, 2, 3, 4]

>>> def adding(l):
……… l.append(5)

>>> adding(l)

>>> l

[1, 2, 3, 4 , 5]

But what happen if it was an immutable variable:

>>> a = 5

>>> def increment(a)

…………. a += 1

>>> increment(a)

>>> a

5

So why it didn’t change? as we said previously integers are immutable witch make it impossible to change it, all we was creating a new variable inside the function witch have the value of 6, but the variable outside the function haven’t been changed at all, so the 5.

--

--