Python Objects: Mutable and Immutable

Brandyn Reindel
3 min readOct 9, 2019

--

Python is an object oriented programming language, which basically means that everything in python is an object. However, not all objects in python share the same characteristics. Every variable in python is an object, which holds an object instance. When you initiate an object it is assigned a unique object id, and at runtime an objects’ type is defined. These types are mutable or immutable. Basically, a mutable object (e.g. list, dict, set) can be changed after it is created, whereas an immutable object (e.g. int, float, tuple) can not. To know for sure if an object is mutable or immutable we need look at id() and type().

id() is a builtin function that returns the identity of an object. It is returned as an integer that corresponds to the memory location of the object. type() is a builtin function that returns the type of an object. The implementation of both these functions is very simple, here’s an example where we use ‘is’ to compare the identity of the two objects.

''' Example 1 '''
>>> x = "Holberton"
>>> y = "Holberton"
>>> id(x)
140135852055856
>>> id(y)
140135852055856
>>> print(x is y) '''comparing the types'''
True''' Example 2 '''
>>> a = 50
>>> type(a)
<class: ‘int’>
>>> b = "Holberton"
>>> type(b)
<class: 'string'>

Lets explore object types even further by exploring mutable and immutable a bit more.

Mutable objects, as I said before, can change its state or contents after it is created. Custom classes and variables are usually mutable as well many builtins such as set, list, and dict. Immutable as you may have guessed are the opposite, these objects cannot be changed or modified after it is created. Lets’s take a look at these examples:

Mutable:

m = list([1, 2, 3])
n = m
id(m) == id(n)m.pop()id(m) == id(n)Now:m = list[1,2]

We create an object of type list with identifiers m and n. Using pop we take away an int form the list, the object id is not changed, but the object is modified.

Immutable:

x = 10
x = y
id(x) == id(y)
id(y) == id(10)
If we do an operation such as:
x = x + 1
id(x) != id(y)
id(x) != id(10)

We create an object of type int with ‘x’ and identifiers ‘x’ and ‘y’ point to this object. After our simple operation, ‘x’ is changed, but object 10 is never changed, immutable.

As you can see python handles mutable and immutable objects differently. Mutable objects are great when you need to change the size or contents of an object. While immutable objects are quicker to access, and are great when you need to ensure that an object will stay the same. Now we have a better understanding of these two types of objects let’s figure out how they are passed to functions.

When passing objects to functions it is important to know the difference between mutable and immutable and when to use each kind, especially in regards to memory efficiency. A mutable object can change the original variable itself. So if called by reference in a function it needs to be copied to another variable. Whereas an immutable object can just be called because the value of the object cannot change. Making it quicker to access and more efficient.

--

--