Python Programming: Immutable and mutable objects

Introduction:

In the python programming language, one will notice that there is more than one way of containing a sequences of data. There are lists, tuples, dictionaries, and sets. These sequences that contain data usually fall into one of two categories in terms of their flexibility. These two categories are mutable and immutable. There are important reasons for why these sequences would be mutable (have the ability for its content to be changed) or immutable(content cannot be changed).

Id’s and Types

First let’s talk about ID’s and types in Python. ID’s are essentially the “identity” of an object. This identity is represented by a sequence of numbers, for example “1130042332” could be a typical ID of a python object. Each object will have its own ID. So, when someone makes a list of integers call numbers, numbers will have an ID number.

A list called numbers, and ID is printed after it is called in the ID function

Now to get into types. There are several different data types in python. There are strings, integers, floats, dictionaries, sets, lists, tuples, etc. These are all types of objects in python. All these types in python are considered objects. Therefore, an integer is an object, while a list is also an object. These types can be categorized in more generalized categories. Numeric types, which consist of integers, floats, long integers, and complex integers. Sequences which include strings, bytes, byte arrays, lists, and tuples. There are sets which include sets and frozen sets. And finally mappings which include dictionaries. With wide variety of types in the python programming language, some are considered mutable while others are immutable. This is what we will be going over next.

Mutable and Immutable Objects

In python, an object is either strictly mutable or immutable. If an object is immutable it means that after the object is created, the contents cannot be changed. Mutable on the other hand means that the contents can be changed.

List of Mutable Types:

Sequences, lists, byte arrays, sets, dictionaries, classes, class instances

List of Immutable Types:

Integers, floats, complex numbers, strings, tuples, frozen sets, bytes

Let’s look a little more deeper into the subject. A string, is considered an immutable object. Therefore, if one was to make a variable called ‘a’ and assigned it to contain “Keplar”, and made another variable called ‘b’ and also gave it the string “Keplar”, both ‘a’ and ‘b’ would actually refer to the same object.

Because they both refer to the same object, both ‘a’ and ‘b’ have the same ID

In the example above, both ‘a’ and ‘b’ have the same ID. Since strings are immutable and cannot be changed to begin with, when one assigns a variable to a string, and assigns another variable to an exact replica of that string, it actually refers to the same object! One can also check if two variable refer to same object by using ‘is’ operator.

a is b = True

Mutable types such as lists can can have their data changed, therefore if we were to assign two lists the exact same data, they would not refer to the same object. So, let us say that we have a list called ‘list_1’ and another called ‘list_2’. They both contain a list of number of let’s say ‘3, 2, 8, 0’. These two lists would not refer to the same object because they are mutable. Lists can have their contents replaced, changed, deleted, or added onto.

Two lists with the same content, but have different addresses.
In this example, the first index of list_1 is being changed, because lists are mutable

In the example above, both lists have the exact same content. However, because lists are mutable, they do not refer to the same object. As you can see, their ID’s are different.

Why is the difference between immutable and mutable objects important?

It is important for python to treat certain objects as immutable and others as mutable because both types have different usages. Mutable types are good for easily passing around data. For example, a list is a mutable type. Two or more objects can have accesses to a change that is made from a list right away. So if there is an object called ‘dog’ and another object called ‘cat’, and a list called ‘pet_foods’, both objects have access to this list and its changes. So, if the object ‘dog’ appended something to the lists, the ‘cat’ object will automatically have access to the change without having to make a copy of the list.

Immutable objects on the other hand are good with working with the data. Immutable types are good for data that will be used again in the future, and because it is unchangeable, it will always be a relevant reference. So, let us say that there is a tuple with important data inside that can be used later on. An object can refer back to this tuple, without worrying about if any of the data inside changed or was modified. Therefore, it is a great tool.

Function Calls and how Mutable/Immutable Objects are related:

In Python, function arguments are passed through reference. This means that arguments passed through functions get an implicit reference to the argument. In other languages such as C, functions call arguments through what is known as call by value, where a copy of the argument is made. However, in call by reference, no copy is made. Therefore, the argument itself can be changed directly through the function call. So by chance, one can accidentally change the value of the argument! With all this being said, one can assume another reason why immutable and mutable objects are important. If arguments are passed by reference in python, then sometimes using immutable objects can be helpful, since they cannot be changed anyways. Therefore, one cannot accidentally change the value of an argument if it is immutable.

Show your support

Clapping shows how much you appreciated Kevin Pak’s story.