Understanding Python’s Data Types: Mutable vs. Immutable Objects

Raphael Schopfer
5 min readOct 19, 2023

--

Introduction

Guido Van Rossum, aged 63, released the first version of the Python programming language in 1991.

Python, a versatile and popular programming language, offers a variety of data types, each with its unique characteristics. Understanding the concept of mutable and immutable objects in Python is crucial for writing efficient and bug-free code. In this comprehensive blog post, we’ll explore the differences between these two types of objects and why it matters in Python.

ID and Type

In Python, every object has an identity and a type. The identity, obtained using the id() function, is a unique identifier for the object, while the type is its class or category. Let’s examine these attributes with some examples: python

Mutable Objects

Mutable objects can be modified after creation. Here is a list of common mutable objects:

  • Lists
  • Dictionaries
  • Sets
  • User-defined classes (if not designed to be immutable)

When two variables refer to the same mutable object, changes made to one will affect the other. Consider the following:

Immutable Objects

Immutable objects cannot be changed after they are created. Here is an expanded list of immutable objects, including additional examples:

  • Numbers (integers, floats)
  • Strings
  • Tuples
  • Frozensets
  • Namedtuples
  • Constants like None

When two variables reference the same immutable object, any changes create new objects rather than modifying the original. For instance:

Assignment vs. Referencing

In Python, it’s essential to understand the difference between assignment and referencing when dealing with mutable and immutable objects. Assignment creates a new reference to an object, while referencing creates another reference to the same object in memory.

How Immutable Objects Are Stored in Memory

Immutable objects are typically stored in a fixed location in memory. Any changes to their values create new objects at different memory locations. This design choice ensures that the original object remains unaltered.

Examples with Memory Schema

To help you visualize the concept, here are two examples with memory schema:

Example 1: Mutable Object (List)

Example 2: Immutable Object (String)

Variable Value Management When Passing to a Function

In Python, arguments are passed to functions by reference. When you pass a mutable object to a function, you’re essentially sharing the same object in memory. Therefore, any changes made within the function affect the original object. For immutable objects, the function operates on a copy of the original object, leaving the original unchanged.

Integer Pre-allocation

Python pre-allocates a range of small integers, known as SMALLPOSINTS and SMALLNEGINTS, for optimization purposes. These integers represent the most commonly used integer values and are pre-allocated to save memory.

Mechanism of Aliases

Aliases are multiple references to the same object. When two variables are aliases, they point to the same object in memory. This concept is important to comprehend how multiple variables can reference the same object, leading to unexpected behavior with mutable objects.

SMALLPOSINTS and SMALLNEGINTS

Python’s SMALLPOSINTS and SMALLNEGINTS are constants representing small integer values that are commonly used. These values are pre-allocated for efficiency.

Why SMALLPOSINTS and SMALLNEGINTS Have Those Values

The values assigned to SMALLPOSINTS and SMALLNEGINTS are determined based on the most frequently used integer values. Python pre-allocates these values to improve performance and save memory.

Special Case of Tuple and Frozen Set

While tuples and frozen sets are considered immutable, they can contain mutable objects. When modifying a mutable object within a tuple or frozen set, it doesn’t change the tuple or frozen set itself but does affect the contained object. This is an essential exception to understand when working with immutable objects containing mutable elements. In Python, understanding the distinction between mutable and immutable objects is crucial for writing efficient, bug-free code. The choice between mutable and immutable data types depends on your use case. Mutable objects allow in-place modifications, but you should be cautious of potential side effects. Immutable objects provide safety and predictability, making your code more robust and maintainable.

Conclusion

Animations with Mayavi

(Mayavi is a Python module for interactive 3D data visualization with a simple interface.)

By mastering these fundamental concepts, you’ll become a more effective Python programmer, capable of creating robust and maintainable code.

Thank you for reading!

RaphSchp

--

--