lh1008
4 min readJan 17, 2020

--

Python3: Mutable, Immutable… everything is object!

Python, like many other languages, is an object-oriented programming language. Objects are a collection of data (also known as variables) and methods, methods are functions that act on that data. Objects are the most important thing in python. Objects can have attributes or properties. You can create as much objects as you want and give them attributes to later use them in those methods (functions).

Every object has an identity, this identity in C is known as the memory address. In python we know it as ‘unique id’ and it’s called by typing the id() function. So whenever you need to know the id of an object you can do this (make sure you have installed python or python3 in your system):

>>> a = 1024

>>> id(a)

139916432349232

You can also know the data type of an object by calling the type() function. There are several object data types in python, some of them are: str , int , float , list , tuple , range , dict , set , bool , just to name some of them. When you make the call of the type() function this is what you receive:

>>> a = 1024

>>> type(a)

<class 'int'>

Mutation

A unique id is given to every object every time is instantiated (created). The state of the object can be changed or it can’t be changed depending if it’s a mutable or an immutable object. Mutable objects are objects that their content can be changed, i.e.:

>>> list = ["Hello", 1, 2, "Bye"]

>>> list[0] = "Changing"

>>> list

["Changing", 1, 2, "Bye"]

These are some mutable objects data types: list, dict, set , etc.

Immutable objects instead can’t be modified:

>>> message = "This is immutable"

>>> message[0] = "change"

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

You will receive an error message.

Immutable objects are built-in data types, some of them are: int , float , bool , string , unique code , tuple , etc.

Mutable means ‘able to change’ and immutable ‘constant’. This is important because this helps you decide what type of data you want to be able to change and which other type of data you don’ t want it to be changed.

This is very important also in memory optimization because when you change a mutable object, you will receive the same object with the same unique id, but when you try to change an immutable object you will receive a new object instead.

Arguments

Arguments are values provided to the function, e.g.:

>>> def add(me):
... me += [3]
... return me

>>> x = [0, 1, 2]
>>> y = add(x)
>>> y
[0, 1, 2, 3]
>>> x
[0, 1, 2, 3]

Arguments, are the values sent to the functions (methods). This is important because when sending a value to a mutable object, this will modify both objects like you can see above, y and x both were modified because we sent a list and list is a mutable object.

This won’t work the same if we send an immutable object. What will happen is that one of the objects will change but the other one will maintain itself the same, they no longer are equal. Let’s check it out:

>>> def add(me):
... me += (3, )
... return me

>>> x = (0, 1, 2)
>>> y = add(x)
>>> y
(0, 1, 2, 3)
>>> x
(0, 1, 2)

You can see that now we have two different objects when we try to change an immutable object, y is now (0, 1, 2, 3) , and x is (0, 1, 2) . A new object y was created.

Objects in python are quite curious, you will see that you can do a lot of things with them. If you check the, “The Zen of Python” you will find that python programming language tries to comply with the one obvious way to do things when creating objects, “There should be one — and preferably only one — obvious way to do it.”

Sources:

--

--

lh1008

Life just keeps on happening in the eternal present. Keep building your present.