🧠 Python DeepCuts — 🧩 Everything Is An Object
Posted On: October 29, 2025
Description:
Python’s philosophy is beautifully consistent: everything is an object.
Numbers, strings, lists, functions, classes — even modules and exceptions — all live within a single, unified object model.
This means every value has:
- an identity (id()) → where it lives in memory
- a type (type()) → what it is
- and is a member of the object hierarchy (isinstance()).
Let’s explore the foundation of Python’s design.
🧩 Everything Is an Object
Every entity in Python — even the ones that look primitive — is an instance of object.
import math
n = 42
s = "hello"
lst = [1, 2, 3]
def greet(name):
return f"Hi, {name}"
class User:
pass
print(isinstance(n, object)) # True
print(isinstance(s, object)) # True
print(isinstance(lst, object)) # True
print(isinstance(greet, object)) # True
print(isinstance(User, object)) # True
print(isinstance(math, object)) # True
Everything in Python passes the isinstance(..., object) check — even the class definitions themselves.
🧭 Identity vs Equality
Python distinguishes between identity (is) and value equality (==).
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True → values are equal
print(a is b) # False → different objects
print(a is c) # True → same reference
print(id(a), id(b), id(c))
Each object gets its own identity (id()), which represents where it’s stored in memory.
🏷️ type() and isinstance()
Every object in Python has a type, and all types themselves derive from object.
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
p = Point(2, 3)
print(type(p)) # <class '__main__.Point'>
print(isinstance(p, Point)) # True
print(isinstance(p, object)) # True
Even user-defined classes ultimately inherit from object.
🧬 Classes Are Objects Too
In Python, classes are instances of type, and type itself is a class — creating a neat circular model.
class A:
pass
print(type(A) is type) # True
print(isinstance(A, type)) # True
a = A()
print(type(a) is A) # True
print(issubclass(A, object)) # True
print(issubclass(type, object)) # True
print(isinstance(object, type)) # True
print(isinstance(type, type)) # True
This means:
- type creates classes.
- object is the ultimate base.
- Both form a self-referential loop that defines Python’s object system.
🧱 The Object Hierarchy (MRO)
Even complex class hierarchies ultimately end with object.
class Base1: pass
class Base2: pass
class Child(Base1, Base2): pass
print(Child.__mro__)
mro (Method Resolution Order) shows the inheritance chain — and it always ends with object.
🧩 Functions Are Objects Too
Functions are first-class objects — they can be passed, assigned, or even tagged.
def square(x):
return x * x
square.tag = "math-op"
def apply_twice(f, value):
return f(f(value))
print(square(5)) # 25
print(square.tag) # 'math-op'
print(apply_twice(square, 3)) # 81
✅ Key Points
- ✅ Everything in Python — numbers, lists, functions, classes, modules — is an object.
- ✅ Every object has an identity (id()), type (type()), and value.
- ✅ object is the root of all classes; type creates classes.
- ✅ Functions and classes are first-class citizens.
- 🧠 This design makes Python flexible, introspective, and consistent.
Code Snippet:
import sys # to show Python version and platform info
import inspect # handy tools for introspection (signature, isfunction, etc.)
# Primitive-looking values
n = 42 # an int object
s = "hello" # a str object
lst = [1, 2, 3] # a list object
# A function object
def greet(name): # define a function (itself an object)
return f"Hi, {name}"
# A user-defined class object
class User:
pass
# A module object (this file's module is __main__ when run as a script)
import math
# Prove: each is an instance of 'object'
print(isinstance(n, object)) # True
print(isinstance(s, object)) # True
print(isinstance(lst, object)) # True
print(isinstance(greet, object)) # True
print(isinstance(User, object)) # True (classes are objects!)
print(isinstance(math, object)) # True (modules are objects)
a = [1, 2, 3] # create a new list object
b = [1, 2, 3] # create another list with same contents
c = a # c references the *same* object as a
print("a == b:", a == b) # True (values are equal)
print("a is b:", a is b) # False (different objects)
print("a is c:", a is c) # True (same object)
print("id(a):", id(a)) # show identity (CPython: memory address)
print("id(b):", id(b)) # different from id(a)
print("id(c):", id(c)) # same as id(a)
class Point:
def __init__(self, x, y):
self.x = x # set attribute x
self.y = y # set attribute y
p = Point(2, 3) # create an instance of Point
print("type(p):", type(p)) #
print("isinstance(p, Point):", isinstance(p, Point)) # True
print("isinstance(p, object):", isinstance(p, object)) # True (everything derives from object)
class A:
pass
# Classes are instances of 'type'
print("type(A) is type:", type(A) is type) # True
print("isinstance(A, type):", isinstance(A, type)) # True
# Instances' type is their class
a = A()
print("type(a) is A:", type(a) is A) # True
# 'object' is the ultimate base class
print("issubclass(A, object):", issubclass(A, object)) # True
# The famous circle: 'type' is a subclass of 'object', and 'object' is an instance of 'type'
print("issubclass(type, object):", issubclass(type, object)) # True
print("isinstance(object, type):", isinstance(object, type)) # True
# And 'type' itself is an instance of 'type'
print("isinstance(type, type):", isinstance(type, type)) # True
class Base1:
pass
class Base2:
pass
class Child(Base1, Base2): # multiple inheritance example
pass
print("Child.__mro__:")
for cls in Child.__mro__: # iterate the MRO
print(" ", cls)
# Every new-style class ultimately ends with 'object'
print("Ends with object:", Child.__mro__[-1] is object)
def square(x): # define a function object
return x * x
square.tag = "math-op" # attach a custom attribute (functions are objects!)
def apply_twice(f, value): # higher-order function (takes a function)
return f(f(value)) # call f twice
print("square(5):", square(5)) # 25
print("square.tag:", square.tag) # 'math-op'
print("apply_twice(square, 3):", apply_twice(square, 3)) # 81
No comments yet. Be the first to comment!