[CS Fundamentals] Mutable vs. Immutable

·

3 min read

[CS Fundamentals] Mutable vs. Immutable

Introduction

This article introduces concepts of mutability and immutability in computer science. It’s very important to understand it because it’s going to help you to solve errors in the future. Especially, beginners often face situations such that program outputs unexpected results. After reading this article, you will have a sight of seeing which variable contains which value.

* This article is based on python.


Immutable vs. Mutable

An immutable value is one whose content cannot be changed without creating an entirely new value, in comparison with mutable variables.

Immutable types:

  • int

  • boolean

  • float

  • string

  • tuple

  • complex

  • bytes

mutable types:

  • list

  • dictionary

  • set

To understand the difference between immutability and mutability in python, you need to focus on two keywords, which are “change” and “id”. After value changes, if that variable holds the same id, it is mutable. If not, it is immutable.

Immutable Example:

x = 1
y = x

y += 3

print(x, y) # 1, 4
print(id(x), id(y)) # 139812355671168 139812355671264 <- DIFFERNET ID

We have x and y, and their types are integer. Integer is immutable, so they point to different id(memory). As I said, each points to different memory after the value of y is changed. Therefore, it is immutable.

x = 1
y = x

y += 9999999999
y = 1

print(id(x), id(y)) # 139771559752832 139771559752832

The ids are the same once the values are the same.


Mutable Example:

arr1 = [1, 2, 3]
arr2 = arr1

print(id(arr1)) # 140535011664432
print(id(arr2)) # 140535011664432

As you might expect, the id is the same. Now let’s have a look at this code:

arr1 = [1, 2, 3]

print(id(arr1)) # 139698565288496
arr1[0] = 9

print(arr1) # [9, 2, 3]
print(id(arr1)) # 139698565288496

The id is the same even though the first index of arr1 is changed to 9. So, we call list mutable. We can change the value!


What are the benefits of immutability?

  • Safety and Predictability: Immutable objects can't be modified accidentally, making the code more predictable and reducing bugs.

  • Hashing and Dictionary Keys: Immutable objects can be used as dictionary keys or set elements because their value remains constant.

  • Easier Debugging: Since immutable objects don’t change, it's easier to track their values and debug the code.

  • Functional Programming: Immutability promotes pure functions (functions that don’t alter their inputs), which makes code easier to reason about and test.

  • Thread-Safety: Immutable objects are inherently thread-safe, so you don’t need locks when multiple threads access them.

  • Cache-Friendly: Since they can't change, immutable objects can be cached more efficiently.

  • Simplified State Management: In applications with complex state management, immutability ensures that data remains consistent across changes.

Did you find this article valuable?

Support Jay's Dev Blog by becoming a sponsor. Any amount is appreciated!