Skip to main content
To write efficient and bug-free Python code, it helps to understand how Python works under the hood. Let’s explore Python’s memory model, object references, and memory management.

Mutability vs. Immutability

Every variable in Python is a reference pointing to an object in memory. Objects are divided into two main categories:

1. Mutable Objects

A mutable object can be changed after it is created. Examples include:
  • Lists (list)
  • Dictionaries (dict)
  • Sets (set)

2. Immutable Objects

An immutable object cannot be changed after it is created. If you try to modify it, Python creates a new object at a new memory address. Examples include:
  • Integers (int), Floats (float)
  • Strings (str)
  • Tuples (tuple)
  • Booleans (bool)
Precaution: Since tuples are immutable, you cannot change their elements. However, if a tuple contains a mutable object (like a list), the list elements can be modified!

Everything is an Object

In Python, everything is a first-class object. This includes integers, strings, functions, modules, and even classes themselves. An object in Python contains three things:
  1. A Value: The data itself (e.g., "Hello").
  2. A Type: Defines what the object can do (e.g., <class 'str'>).
  3. An Identity: A unique integer representing its memory address (retrieved via id()).

Identity (id()) vs. Equality (==)

Understanding the difference between == and is is a common source of confusion:
  • Equality (==): Compares the values of the objects (uses the __eq__ method).
  • Identity (is): Compares the memory addresses of the objects (checks if both variables point to the exact same object in memory).
Caching Nuance (Integer Caching): Python caches small integers (from -5 to 256) and small strings in memory for optimization.

Reference Counting

Python manages memory automatically using garbage collection. The primary mechanism used is Reference Counting.
  • Every object in memory keeps track of how many variables are referencing it.
  • When you assign an object to a variable, its reference count increases by 1.
  • When a variable goes out of scope or is deleted, the reference count decreases by 1.
  • When the reference count drops to 0, the object is destroyed, and the memory is reclaimed.
Circular References: If Object A references Object B, and Object B references Object A, their reference counts can never drop to 0 even if they are unreachable. Python uses an auxiliary Generational Garbage Collector to detect and destroy these circular references.

Practice & Exercises

To reinforce what you’ve learned in this section (Mutability, object types/identities, caching optimizations, and reference counting), practice with these interactive notebooks:

Follow-Along Practice

Practice defining mutable and immutable objects, tracking id values, appending mutable sub-items inside tuples, comparing equal values vs identities, and tracking reference counts.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on extending mutable lists, editing nested tuple elements, evaluating dictionary identities, and verifying small integer cache limits.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Now that you understand Python’s internal memory structures, let’s learn how functions are treated as objects, closures, and writing custom decorators!

Advanced Functions

Master lambdas, closures, and decorators