Mutable vs Immutable Objects in Python

Stiven Gonzalez
4 min readMay 21, 2021

--

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

Objects: identifier, type and value

In Python, every piece of data (number, string, list, etc.) that appears in a program is an object. Each object has an identifier, a type, and a value:

Identifier:

Each object has a unique identifier that can be found using the id () function:

Type:

Each object has a type that can be found using the type () function :

Value:

The value that the object contains is the data itself.

Mutable vs Immutable Objects

Some objects contain references to other objects, these objects are called containers. Some examples of containers are a tuple, list, and dictionary. The value of an immutable container that contains a reference to a mutable object can be changed if that mutable object is changed. However, the container is still considered immutable because when we talk about the mutability of a container only the identities of the contained objects are implied.

  • Some of the mutable data types in Python are list, dictionary, set and user-defined classes.
  • On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and range.

Variables and immutable objects

Because immutable objects cannot be modified, when modifying variables that refer to immutable objects, the variables always refer to other objects.

More on creating and destroying objects

  • Mutable objects are all different, that is, Python creates objects every time we use data.
  • In the case of immutable objects, Python sometimes creates distinct objects and sometimes not. For example, in the case of small integers (or strings) just create one object:
  • But in the case of decimals, create different objects.
  • Tuples are immutable objects, that is, they cannot be modified:
  • But if a tuple includes mutable objects, even if an error occurs trying an immutable object, the mutable object is modified:

Objects passed to functions

Here we want to know more about how arguments are passed to functions and what that implies for mutable and immutable objects.

Here’s the definition from the book:

Python uses a mechanism, which is known as “Call by Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”.

Immutable:

If you pass immutable arguments such as integers, strings, or tuples to a function, the step works as a call by value.

The referenced object is passed to the parameters of the function. Inside, they cannot be modified, so they cannot be changed at all, that is, they are immutable.

>>> def ref_demo(x):
... print "x=",x," id=",id(x)
... x=42
... print "x=",x," id=",id(x)
...
>>> x = 9
>>> id(x)
41902552
>>> ref_demo(x)
x= 9 id= 41902552
x= 42 id= 41903752
>>> id(x)
41902552

They are often transferred through an object relationship, but in function, they can be modified instead.

If we pass a list to a function, we have to remember two cases:

  • It is possible to modify the elements of the list instead, that is, the list can be changed even within the scope of the caller.
  • If the name is assigned to a new list, the existing list will not be changed, that is, the list outside the control of the caller will remain unchanged.
>>> def ref_demo(l):
... print l
... l += [47,11]
... print l
...
>>> list_1 = [1, 2, 3]
>>> ref_demo(list_1)
[1, 2, 3]
[1, 2, 3, 47, 11]
>>> print list_1
[1, 2, 3, 47, 11]

--

--