Python - Immutable & Mutable

🔒 What is Immutability in Python?

Immutability means something that cannot be changed after it's created.
In Python, strings, integers, tuples, etc. are immutable types.

When you create a string and assign it to a variable, that string cannot be changed—any change results in the creation of a new object in memory.

🧠 How Python Stores Variables in Memory

Think of a variable like a label or pointer 🏷️ that sticks to a value stored somewhere in memory (💾). When you assign a value to a variable, Python makes that variable point to a memory location that holds the actual value.

Example:

username = "msahoo"

At this point, Python stores the string "msahoo" in memory, and the variable username simply points to that memory location.

📌 Now username → "msahoo".

🔁 What Happens When You Reassign an Immutable Variable?

Let’s see what happens if we reassign the variable:

username = "hello"

Here’s the magic of immutability:

  • Python doesn't change the original "msahoo" string.

  • Instead, it creates a new string object "hello" in memory.

  • Then, it re-points the username variable to this new string.

So:

  • Before: username → "msahoo"

  • After: username → "hello"

🧠 The "msahoo" string is still sitting in memory—but no variable is pointing to it now.

🔄 Memory Reference Flow (Visual Explanation)

Your example in the image is a perfect representation of how it works:

Step 1:

username = "msahoo"

📌 Memory:

username ───▶ "msahoo"

Step 2:

username = "hello"

📌 Memory:

username ───▶ "hello"
"msahoo" (orphaned, waiting for garbage collection)

🧹 Python’s Garbage Collection (🗑️)

Once a value (like "msahoo") is no longer referenced by any variable, Python’s garbage collector will clean it up automatically.

You don’t have to manually delete it—Python handles it for you in the background. That’s why we say Python has automatic memory management.

💡 Why is Immutability Important?

Here’s why Python makes some objects immutable:

  • Safety: No one can change the object by mistake.

  • Performance: Immutable objects are optimized internally.

  • 🧵 Thread-Safe: Multiple parts of the code can use the same immutable object without issues.

It’s like working with a locked file (🔒)—you can read it, but you can't write over it.

🧪 Final Recap with Code:

username = "msahoo" # step 1 - new string created
username = "hello" # step 2 - new string created, old one orphaned
  • "msahoo" and "hello" are two different objects.

  • username just points to different objects in different steps.

  • Old object gets garbage collected if nothing points to it.

❓ Why are we saying “object” and not “variable”?

In Python, everything is an object—strings, numbers, lists, etc.
A variable is just a label that refers to an object.

So, when we say:

“Immutable object is thread-safe,”
we mean:
“The actual thing in memory (like a string or number) is safe to share between threads because it can’t be modified.”

10
Subscribe to my newsletter

Read articles from MRUTYUNJAYA SAHOO directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MRUTYUNJAYA SAHOO
MRUTYUNJAYA SAHOO

Passionate about programming, I see its impact everywhere in the modern world. I believe that I will be a great addition to the team and organization. Making a positive impact through hands-on experience and continuous learning.