Mutable vs Immutable in Python

Python is an object oriented programming language. This means that things such as integers, floats, functions, and strings are all objects in the sense that it can be assigned to a variable or passed as an argument to a function. Variables are simply references and don’t actually hold data themselves, kind of like a pointer.

Types and id : Each object in python has an identifier, a type, a value and a class it belongs to. The “ id ” is unique to every object. This corresponds to the location of the object in memory.

In order to obtain the identification of an object, the built in function id(object) is used which accepts one parameter.

>>> id(variable)

139919432649235

You can also know the data type of an object by calling the The builtin function type(). Like the identifier, the type of an object cannot be changed and it uses one parameter.

The id of an object is used to help differentiate between two variables being identical and them being linked to the same object. We can use “==” to determine if their value are equal , while “is” can be used to determine if both variables are pointing to the same object.

Mutable and Immutable objects

Every object in Python is classified as either immutable (unchangeable) or not.

mutable objects : Mutable Objects are the objects capable of accepting change. This means that their items can be altered, removed or changed. Some built-in mutable types in Python are: lists, sets and dicts.

Immutable Objects: On the other hand, Immutable Objects are the objects that cannot accept changes. This means that these objects will need to hold the same variable and does not take changes for it’s content or it’s type. Simply, if you try to change an element in an immutable set, however. The interpreter will raise an exception TypeError. Great examples of immutable objects could be (ex. integer, tuple, string).

Since Python must create a separate object for each unique immutable value, which takes up a lot of memory, the interpreter optimizes by creating objects.

And even more Python stores all common small int objects (-5 to 256) in an array for easy reference. Unlike other objects, which are created as needed and destroyed once the variables referencing that object are deleted.

why does it matter ?

This distinction between Mutable and Immutable turns out to be crucial in Python work. In fact, immutability can be used to guarantee that an object remains constant throughout your program; mutable objects’ values can be changed at any time and place (and whether you expect it or not).

Although if we want to modify a mutable object such as lists and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning. The easiest way to clone a list is to use the slice operator:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store