Sitemap

Everything is an Object in Python

2 min readJul 25, 2025

--

Press enter or click to view image in full size

Introduction

In Python, everything is an object. At first, this sounded abstract to me, but after going through this project, it makes a lot more sense. Whether it’s a number, a list, a string, or even a function, they all behave as objects. This affects how Python stores data, manages memory, and handles variables.

id and type

Every object in Python has a unique identity and a type. The id() function gives you the memory address (in CPython) and type() tells you what kind of object it is. Two objects can have the same value but different ids.

a = 5
b = 5
print(id(a) == id(b)) # True (because Python reuses small integers)

x = [1, 2]
y = [1, 2]
print(id(x) == id(y)) # False

Mutable Objects

Mutable means the object can change. Lists, dictionaries, and sets are mutable. If you pass a list to a function and change it, the original list changes too.

my_list = [1, 2]
my_list.append(3)
print(my_list) # [1, 2, 3]

Get Abdullah Alameeri’s stories in your inbox

Join Medium for free to get updates from this writer.

Immutable Objects

Immutable objects cannot change after creation. Numbers, strings, and tuples are immutable. If you “change” them, Python creates a new object.

a = "Hello"
a += " World"
print(a) # "Hello World"

Even though it looks like the original string changed, it didn’t — a new string was created.

Why it matters

It matters because it can lead to bugs if you don’t understand how Python handles these objects. For example, using += on a list changes it in-place, but using + creates a new object.

a = [1, 2]
b = a
a += [3]
print(b) # [1, 2, 3]

a = [1, 2]
b = a
a = a + [3]
print(b) # [1, 2]

Function arguments

When passing arguments to functions, Python sends a reference to the object. If it’s mutable, changes inside the function will affect the original. If it’s immutable, it won’t.

def change_val(n):
n += 1

a = 10
change_val(a)
print(a) # 10

def modify_list(lst):
lst.append(100)

my_list = [1, 2]
modify_list(my_list)
print(my_list) # [1, 2, 100]

Conclusion

This project helped me better understand how Python deals with objects. Knowing the difference between mutable and immutable types, and how Python passes variables to functions, makes a big difference when writing code that behaves the way I expect.

--

--