Sitemap
elman

elman

Python — Everything is object

4 min readJun 8, 2026

--

Press enter or click to view image in full size

If you are into programming nowadays, you probably hear about Python everywhere. It is the king of Machine Learning, Data Science, Backend development, and even robotics. People love Python because it has a clean syntax, a massive community, and it is very easy to learn. But have you ever wondered how Python works behind the scenes?

In Python, there is a golden rule: Everything is an object. Whether you write a function, a string, or a simple number, Python converts it into an object in memory. Every object in Python has a “passport” with three important things: a Value (the data inside), a Type (what kind of data it is), and an ID (its unique address in memory).

We can check these using type() and id() functions:

a = 89
print(type(a)) # Output: <class 'int'>
print(id(a)) # Output: 140733276682120 (Memory location)

But here is the real catch: Python divides these objects into two main groups: Mutable and Immutable

Press enter or click to view image in full size

Mutable Objects (The Changeable Ones)

Mutable objects are objects that you can change after creating them. The most common examples are lists, dictionaries, and sets. When you change a mutable object, Python updates the data directly inside the same memory address. The ID does not change. Think of it like rearranging the furniture inside your house; the house address stays exactly the same.

my_list = [1, 2, 3]
print("Before:", id(my_list)) # Example ID: 1405328

my_list.append(4)
print("After:", id(my_list)) # ID is still exactly: 1405328!

So, if you createlist2 = my_list, any change you make to my_list will also change list2. They both look at the exact same box in memory!

Immutable Objects (The Unchangeable Ones)

Immutable objects are the exact opposite. Once they are created, their values can never be changed. This group includes integers, floats, strings, and tuples.

Get elman’s stories in your inbox

Join Medium for free to get updates from this writer.

You have to ask: “If I write x = 10 and then x += 1, doesn't x change to 11?" Yes, it does, but not the way you think! Python doesn't change the number 10 into 11. Instead, it creates a brand-new object 11 somewhere else in memory and moves your variable x to point to this new address.

x = 10
print("Initial ID:", id(x)) # Old address

x += 1
print("New ID:", id(x)) # Totally different address!

Why Does This Difference Matter?

Python treats these two types differently to optimize performance and save computer memory. Since immutable objects never change, Python safely reuses them. For example, if you create a = 10 and b = 10 in different parts of your code, Python doesn't waste memory by creating two separate tens. It connects both a and b to the same cached 10 object.

But Python cannot do this for mutable objects. If you create l1 = [1, 2] and l2 = [1, 2], Python will create two completely different boxes in memory. Why? Because you might modify l1 later, and Python needs to make sure l2 stays safe.

Press enter or click to view image in full size

How arguments are passed to functions and what does that imply for mutable and immutable objects?

The ultimate test of this topic happens when you pass variables into functions. Python uses a system called Pass-by-Object-Reference. This means Python sends the real memory address of the object to the function. However, the final result depends on the object’s type:

  • If you pass an immutable object (like an integer) and change it inside the function, the original variable outside stays safe and unchanged.
  • If you pass a mutable object (like a list) and modify it inside the function, the original list outside changes too!
def test_func(number, my_list):
number += 1
my_list.append("New Element")

n = 5
l = [1, 2]

test_func(n, l)

print(n) # Output: 5 (Did not change because int is immutable)
print(l) # Output: [1, 2, 'New Element'] (Changed because list is mutable!)

In short

Understanding how Python manages memory with mutable and immutable objects is important. It helps you write faster code, avoid strange bugs, and most importantly your next Python technical interview!

--

--