What Is an Object in Python, and Why Does Python Use Objects?

Who can read this article?
This article is intended for readers who already have a basic understanding of Python, including:
Data types and data structures
Variables
Classes and objects
Attributes and methods
The following topics are enough to understand this article:
Python Language
│
├── 1. Fundamentals
│ ├── Variables & Data Types
│ │ ├── Numbers (int, float)
│ │ ├── Text (str)
│ │ └── Booleans (bool)
│ ├── Control Flow
│ │ ├── Conditionals (if, elif, else)
│ │ └── Loops (for, while, break, continue)
│ └── Input & Output
│ ├── print() & input()
│ └── f-strings
│
├── 2. Data Structures
│ ├── Lists
│ ├── Tuples
│ ├── Dictionaries
│ └── Sets
│
├── 3. Functions
│ ├── Definitions & Returns
│ ├── Arguments (*args, **kwargs)
│ └── Scope (local, global)
│
├── 4. Object-Oriented Programming
│ ├── Classes & Instances
│ ├── Attributes & Methods <----- Enough for this article
│ ├── Constructors (__init__)
│ ├── Inheritance
│ └── Memory & Deletion (del, gc)
│
└── 5. Files & Error Handling
├── Exception Handling (try, except)
└── File I/O (open, read, write, with)
Everything in Python is an object, whether it is an int, float, list, tuple, dict, function, or even a class itself.
What really is an object?
Objects in Python are runtime entities that occupy memory while your program is running.
Every object has three fundamental properties:
Identity (
id())Every object has a unique identity during its lifetime. You can inspect it using
id(). In CPython, this is usually the object's memory address.Type (
type())Every object has a type associated with it. The type acts like a blueprint that defines what operations and behaviors the object supports.
Value
Every object stores a value (or state).
These three properties are what make something an object in Python.
Why does Python use objects?
One of Python's core design principles is that almost everything is represented as an object. This gives Python a simple and consistent programming model.
Why?
Universal consistency
Since almost everything is an object, Python can manage different kinds of data in a uniform way. Every object has an identity, a type, and a value.
Flexibility
Because Python treats almost everything as an object, many language features work consistently across different types of objects.
Bundles data and behavior
Objects combine data (attributes) and behavior (methods) into a single unit. This models real-world or logical entities cleanly.
First-class citizens
Since functions and classes are also objects, they can be treated as first-class citizens. You can:
Pass functions as arguments to other functions.
Store functions inside lists or dictionaries.
Return functions from other functions (this enables decorators).
Assign custom attributes directly to functions or classes.
Important questions to be answered
How can we create objects in Python?
Many beginners think objects are created only when they write something like this:
class Main:
pass
m = Main()
Here, m is an object (instance) of the class Main.
However, objects are much more than just class instances.
Whenever you write:
x = 6 # 6 is an integer object. x is a variable that refers to that object.
y = [2, 3, 3] # The list is also an object.
def func1():
pass # Functions are objects too.
class Main:
pass # The class Main is itself an object.
You can verify this using:
print(id(Main))
print(type(Main))
print(Main)
print(id(func1))
print(type(func1))
In fact, you can verify almost any object using:
id(<object>)
type(<object>)
Interesting fact: Even the built-in
print()function is an object. In fact, every function, including built-in functions, is an object.
So, how are objects created?
One way is by creating your own classes and then creating instances of those classes.
class Harsh:
pass
h = Harsh()
But Python also creates objects when you use built-in types:
x = 2 # int object
y = 3.2 # float object
z = [5, 2, 2] # list object
a = int(2)
Both x = 2 and a = int(2) create an integer object. The literal syntax (2) is simply shorter and more convenient, which is why it is commonly used.
Besides the objects you create yourself, Python also creates many internal objects while your program runs. These objects are necessary for the interpreter to execute your program, even though you usually don't interact with them directly.
How can we delete objects?
You can remove a reference to an object using del.
class User:
pass
u = User()
del u
Important:
delremoves the variable (u), not necessarily the object itself.
The object is destroyed only when there are no remaining references to it. In CPython, this usually happens immediately because of reference counting.
To understand what "references" and Memory Management in Python you can refer to this article:


