HomeAboutSoftwarePublicationsPostsMicroBinfie Podcast

Mutability and immutability in python

Posted on August 28, 2023
an AI generated picture (Midjourney) with prompt; 'computer programming in the style of Robert Motherwell'. You can share and adapt this image following a CC BY-SA 4.0 licence.

This post is part of a series that summarised a workshop we ran recently. We were discussing programming, discussing common pitfalls, and then looked at some fun python tricks.

There are several sections:

Mutability and immutability in Python

In Python, variables can hold different types of data, and these types can be broadly categorized into two groups: immutable and mutable. The distinction between them is related to whether the data stored in the variable can be changed after creation.

  1. Immutable Types: Immutable types are those whose values cannot be modified after they are created. If you want to change the value of an immutable object, you actually create a new object with the modified value. Immutable objects are safe from unintended side effects because they can't be changed in place.

Common examples of immutable types in Python include:

  • Integers (int)
  • Floating-point numbers (float)
  • Strings (str)
  • Tuples (tuple)
  • Frozensets (frozenset)

Example:

python
x = 5
y = x # y points to the same value as x
x = 10 # Creates a new object with the value 10, y still holds 5
  1. Mutable Types: Mutable types are those whose values can be modified after they are created. This means you can change the internal state of the object without creating a new one. However, this mutability can lead to unexpected side effects if you're not careful.

Common examples of mutable types in Python include:

  • Lists (list)
  • Dictionaries (dict)
  • Sets (set)
  • Custom objects (if their class allows modification of attributes)

Example:

python
list1 = [1, 2, 3]
list2 = list1 # list2 points to the same list as list1
list1.append(4) # Modifies the existing list, list2 also reflects the change

It's important to understand the difference between immutable and mutable types because it affects how you work with data in Python. Immutable objects are often used for things like keys in dictionaries and elements in sets, where the value shouldn't change once added. Mutable objects are useful when you need to modify or update data structures over time, but they require more careful consideration to avoid unexpected behavior.

Remember that some types, like lists, can contain both mutable and immutable elements. For example, you can have a list of integers (immutable) inside a list (mutable). This adds another layer of complexity when working with nested data structures.

Why make this complicated? Why can't everything be immutable then? Well, for one, Immutable types are designed for efficient memory management and better speed.

Questions or comments? @ me on Mastodon @happykhan@mstdn.science or Twitter @happy_khan

The banner image is an AI generated picture (Midjourney) with prompt; 'computer programming in the style of Robert Motherwell'. You can share and adapt this image following a CC BY-SA 4.0 licence.