Anik Sikder
Technical Writing/python/everything-is-an-object-in-python-a-deep-dive-into-pythons-object-model
article.sh

$ open article

python

Everything is an Object in Python: A Deep Dive into Python’s Object Model

6 min readAugust 9, 2025
Python Object Model and Metaclass Architecture Visualization

Hey Python newbies and curious cats! 👋

You've probably heard the legendary phrase:

"Everything is an object in Python."

But what does that actually mean?

Why are numbers objects?

Why are functions objects?

How can classes themselves be objects?

And what exactly is a metaclass?

By the end of this article, you'll understand one of Python's most important concepts and gain a deeper appreciation for how Python works behind the scenes.


What Is an Object in Python?

An object is a piece of data that contains:

  • Identity
  • Type
  • Value

Everything that exists at runtime in Python is represented as an object.

Example:

code
x = 5

The value 5 is not merely a number.

It is an object created from Python's built-in int class.

code
print(type(x))
# <class 'int'>

Objects can expose attributes and methods:

code
print(x.bit_length())

Output:

code
3

Because binary 101 requires three bits.


Everything Has a Type

Every Python object belongs to a class.

code
print(type(42))
print(type("hello"))
print(type([1, 2, 3]))
print(type({"name": "Anik"}))

Output:

code
<class 'int'>
<class 'str'>
<class 'list'>
<class 'dict'>

The type determines:

  • Available methods
  • Supported operations
  • Object behavior

Classes Are Objects Too

Here's where things become interesting.

Consider:

code
class Person:
    pass

Most beginners think classes are simply blueprints.

But in Python:

code
print(type(Person))

Output:

code
<class 'type'>

This means:

Classes are objects.

The class itself exists in memory as an object.


Why Does This Matter?

Because classes can be treated exactly like other objects.

You can:

Assign Them to Variables

code
Alias = Person

print(Alias)

Pass Them as Function Arguments

code
def create_instance(cls):
    return cls()

instance = create_instance(Person)

Return Them from Functions

code
def get_model():
    return Person

Create Them Dynamically

code
DynamicUser = type(
    "DynamicUser",
    (),
    {
        "role": "admin"
    }
)

print(DynamicUser.role)

Output:

code
admin

Understanding type()

Most developers use:

code
type(obj)

to inspect object types.

However, type itself is also an object.

code
print(type(int))
print(type(str))
print(type(list))

Output:

code
<class 'type'>
<class 'type'>
<class 'type'>

This means:

type creates classes.


Meet the Metaclass

A metaclass is simply:

A class that creates classes.

The default Python metaclass is:

code
type

When Python executes:

code
class User:
    pass

Internally Python does something similar to:

code
User = type(
    "User",
    (),
    {}
)

The Mind-Bending Part: Why Is type(type) Equal to type?

Try this:

code
print(type(type))

Output:

code
<class 'type'>

At first glance this looks impossible.

How can something be an instance of itself?


Python's Bootstrap Process

The answer lives inside CPython.

When Python starts:

  1. The interpreter creates core objects manually in C.
  2. The type object is constructed.
  3. Python establishes a self-referential relationship.
  4. The object system becomes operational.

This process is called:

Bootstrapping

Without it, Python's object system could not initialize itself.


A Real-World Analogy

Imagine a factory that builds robots.

Normally:

  • Factory → builds robots

But what if the factory itself is also a robot?

Engineers first build the factory manually.

After that:

  • Factory builds robots
  • Factory can build robot versions of itself

This is essentially how Python bootstraps type.


Identity, Type, and Value

Every Python object contains three fundamental characteristics.

Identity

Unique memory reference.

code
x = 10

print(id(x))

Type

Object classification.

code
print(type(x))

Output:

code
<class 'int'>

Value

Actual stored data.

code
x = 10

Value:

code
10

Functions Are Objects

Functions are first-class citizens in Python.

code
def greet():
    print("Hello")

You can attach attributes:

code
greet.language = "English"

print(greet.language)

Output:

code
English

You can also:

  • Pass functions
  • Return functions
  • Store functions inside objects

This capability powers:

  • Decorators
  • Callbacks
  • Functional programming

Modules Are Objects

Even imported modules are objects.

code
import math

print(type(math))

Output:

code
<class 'module'>

Instances Are Objects

Classes create object instances.

code
class Person:
    def __init__(self, name):
        self.name = name

user = Person("Alice")
code
print(type(user))

Output:

code
<class '__main__.Person'>

Introspection: Exploring Objects at Runtime

Python provides powerful introspection tools.

code
type()
id()
dir()
isinstance()
hasattr()
getattr()

Example:

code
user = Person("Alice")

print(dir(user))

This helps developers inspect objects dynamically.


Why Understanding Python Objects Matters

Understanding Python's object model helps with:

  • Object-Oriented Programming
  • Django development
  • FastAPI development
  • Framework internals
  • Debugging
  • Performance optimization
  • Metaprogramming
  • Technical interviews

Many advanced Python concepts become easier once you understand how objects work.


Frequently Asked Questions

What does "Everything is an Object in Python" mean?

Every value in Python is represented as an object, including integers, strings, functions, classes, and modules.


Are integers objects in Python?

Yes.

code
x = 42

print(type(x))

Output:

code
<class 'int'>

Are strings objects in Python?

Yes.

Strings are immutable objects of type str.


Are functions objects in Python?

Absolutely.

Functions can store attributes and be passed around like any other object.


Are classes objects in Python?

Yes.

Classes are objects created by the metaclass type.


What is a metaclass in Python?

A metaclass is a class responsible for creating other classes.

Python uses type as the default metaclass.


Why is type(type) equal to type?

Because Python bootstraps its object system and establishes a special self-referential relationship for the type object.


What is introspection in Python?

Introspection allows developers to inspect object information at runtime using tools such as:

code
type()
id()
dir()
isinstance()

Are modules objects in Python?

Yes.

Imported modules are instances of Python's module type.


Is Python fully object-oriented?

Python is highly object-oriented, but it also supports procedural, functional, and metaprogramming paradigms.


Why should every Python developer understand the object model?

Because it forms the foundation of:

  • Classes
  • Inheritance
  • Framework internals
  • Metaprogramming
  • Advanced Python development

Key Takeaways

  • Everything in Python is an object.
  • Every object has identity, type, and value.
  • Numbers, strings, functions, modules, and classes are objects.
  • Classes are objects created by the metaclass type.
  • Functions are first-class objects.
  • Python's object model powers inheritance and introspection.
  • Understanding the object model is essential for mastering Python.

About the Author

Anik Sikder is a Software Engineer specializing in Python, Django, FastAPI, Cloud Infrastructure, DevOps, and Software Architecture.

He writes about Python Internals, System Design, Distributed Systems, JavaScript, Cloud Computing, and scalable software engineering practices.

$ tags

pythonpython-internalsobject-modelmetaclassoopsoftware-engineeringbackend-developmentdjangofastapiprogramming

$ ls related_articles

status: end_of_file