Python3 : Mutable, Immutable… Everything is object !
Introduction
In Python, everything is an object. One key thing that beginners and even intermediate developers need to fully understand is the difference between mutable and immutable objects. This topic is very important because it affects how your programs behave and how Python manages memory under the hood. In this post, I’ll walk through these concepts with examples from the project I worked on.
ID & Type
Each object in Python has an ID and a type.
The ID is like the address of the object in memory.
The type tells us what kind of object it is (like int, list, str, etc.).
You can check an object’s ID with id() and its type with type():
x = 42
print(id(x)) # Example output: 140710292004496
print(type(x)) # Output: <class 'int'>Even if two objects seem identical in value, they might have different IDs if they are not the same object.
Mutable Objects
Mutable objects are objects that can be changed after they are created.
Examples of mutable types in Python include lists, dictionaries, sets, and byte arrays. Here’s a quick example:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]As you can see, we added an element to my_list, and it changed directly without creating a new list.
Immutable Objects
Immutable objects, on the other hand, cannot be changed after they are created.
Some common immutable types are integers, floats, strings, tuples, and frozensets.
Even though a tuple is immutable, it can contain mutable elements inside it (like a list), but the tuple structure itself (the container) can’t change.
Example of immutable behavior:
my_string = "Hello"
new_string = my_string + " World"
print(new_string) # Output: "Hello World"Here, adding “ World” to my_string didn’t actually change my_string; it created a new string and assigned it to new_string.
Get Kenny’s stories in your inbox
Join Medium for free to get updates from this writer.
Also, for optimization, Python pre-allocates small integers (this is called integer preallocation). It means numbers between certain ranges (like -5 to 256) are reused in memory for performance.
Why It Matters
The distinction between mutable and immutable objects really matters because it affects how Python handles memory and variable references.
- When you modify a mutable object, every variable that references that object will see the change.
- When you modify an immutable object, Python actually creates a brand new object.
This is important not only for writing bug-free code but also for writing efficient code.
How Arguments Are Passed to Functions
In Python, function arguments are passed by assignment (also sometimes called “pass by object reference”).
This behavior makes a big difference depending on whether the object is mutable or immutable.
If you pass a mutable object and modify it inside the function, the changes will affect the original object:
def modify_list(my_list):
my_list.append(4)
my_list = [1, 2, 3]
modify_list(my_list)
print(my_list) # Output: [1, 2, 3, 4]But if you pass an immutable object and try to change it, the original will stay the same:
def modify_string(my_str):
my_str += " World"
my_str = "Hello"
modify_string(my_str)
print(my_str) # Output: "Hello"Here, my_str remains unchanged outside the function because strings are immutable.
Assignment vs Referencing
In Python, when you assign a variable to another, you’re not creating a new object — you’re just creating another reference to the same object.
Example with a mutable object:
list_a = [1, 2, 3]
list_b = list_a
list_b.append(4)
print(list_a) # Output: [1, 2, 3, 4]Both list_a and list_b point to the same list in memory, so changes through one are seen in the other.
Now, compare it with immutable objects:
str_a = "Hello"
str_b = str_a
str_b += " World"
print(str_a) # Output: "Hello"Here, str_a stays the same even though str_b was "modified" — because str_b actually pointed to a brand new string after the change.
BONUS: Aliases
Aliases happen when two (or more) variables point to the same object. With mutable objects, any changes done through one alias will affect the others:
a = [1, 2, 3]
b = a # 'b' is an alias for 'a'
b.append(4)
print(a) # Output: [1, 2, 3, 4]Understanding aliases is important to avoid unexpected bugs in your programs.
Conclusion
Learning about mutable and immutable objects in Python opened my eyes to how deeply it impacts program behavior, memory management, and performance. Whether it’s through how function arguments work, how assignment really works, or how Python optimizes small numbers, understanding these core ideas is critical for becoming a better Python developer. It also helps in debugging tricky bugs related to shared references. Grasping this difference truly makes you feel like you understand Python on a deeper level!





