Garthus
2 min readJan 12, 2021

--

Python3: Mutable, Immutable… everything is object!

Introduction:

We are going to see how to use id() and type() functions and what they told us. What are mutable and immutable objects, why does it matter and how differently does Python treat mutable and immutable objects. And finally, how arguments are passed to functions and what does that imply for mutable and immutable objects

id() and type() :

The id() function returns a unique id for the specified object. All objects in Python has its own unique id. The id is assigned to the object when it is created.The id is the object’s memory address, and will be different for each time you run the program. We can see in the example below how to use it.

The id function return the unique id of the object a

The type() function returns the type of the specified object. Using type() command, you can pass a single argument, and the return value will be the class type of the argument given, example: type(object). You can pass three arguments to the function too, in such case it will return you a new type object.

The type function return the class of the object a

mutable and immutable objects :

The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.

Some of the mutable data types in Python are list, dictionary, set and user-defined classes, once modified they retrieve the same object id as before the modification.

mutable object

On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and range. when we ask Python to modify an immutable object that is bound to a certain name, we actually create a new object and bind that name to it

immutable object

--

--