Class Initialization in Python
When we create an object from a class in Python, we often think of that action as a single step — but Python is actually doing two things behind the scenes:
- It creates the new object.
- Then it initializes that object’s internal state.
These two phases — instantiation and initialization — happen every time we write something like user = User().
This post is about what those phases mean, what Python does automatically, and how we can hook into the process using the __init__ method. It’s not just a convention — __init__ is Python’s built-in way of letting us customize how objects are initialized right after they’re created.
I’ll use a simple User class throughout to keep examples clear and practical. Let’s start by looking at what Python actually does when we instantiate a class.
What Happens When We Instantiate a Class
Python classes are callable. That means when we write something like User(), Python isn’t just returning an object — it’s running a sequence of steps to produce and prepare that object.
Let’s define a simple class:
class User:
role = "member"user = User()Here’s what happens when User() is called:
- Python creates a new instance of
User. This is just a fresh object in memory. - An empty instance namespace is initialized. This is where attributes unique to that object will live — it starts as an empty dictionary.
We can confirm that the object is created by printing or inspecting it:
print(user)
# <__main__.User object at 0x...>At this point, the object exists — it’s an instance of User — but it doesn’t yet have any instance-specific data. Unless we explicitly add something, it only inherits from the class.
That’s where __init__ comes in.
The Role of __init__: Initialization After Creation
Python gives us a way to set up an object right after it’s created: the __init__ method.
Let’s define it inside our User class:
class User:
def __init__(self, name):
self.name = nameNow when we create an instance:
user = User("Jayant")Python runs two steps under the hood:
- It creates the
Userobject (this is instantiation). - It calls
__init__on that object with"Jayant"as an argument (this is initialization):
User.__init__(user, "Jayant")That’s why __init__ is often referred to as the initializer. It’s not responsible for creating the object — it’s just configuring it after creation.
Inside __init__, we typically assign values to instance attributes using self. These assignments don’t go into the class — they go into the new object’s own namespace.
self and the Instance Namespace
Inside __init__, self is the newly created object. When we assign attributes to self, we’re populating that object’s personal namespace.
Get Jayant Nehra’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s expand our example:
class User:
role = "member" # Class-level attribute
def __init__(self, name, email):
self.name = name
self.email = emailNow:
user = User("Jayant", "Jayant@example.com")
print(user.__dict__)
# {'name': 'Jayant', 'email': 'Jayant@example.com'}Here’s what’s happening:
rolelives in the class, so it’s shared across all users unless overridden.nameandemailare set on the instance, and they show up in the instance’s__dict__— that’s its internal namespace.
If we try to access role, Python will first look in the instance’s __dict__, not find it, and then fall back to the class:
print(user.role)
# memberBut name and email are found directly on the instance.
This is the pattern we rely on when writing most Python classes: use __init__ to populate each object’s unique state, while using class attributes for shared defaults or constants. Let's look at a full example and compare this to a two-step setup process without __init__.
Example: Tracking User Info with __init__
Let’s bring this all together with a complete example:
class User:
def __init__(self, name, email):
self.name = name
self.email = email
user = User("Jayant", "Jayant@example.com")
print(user.__dict__)
# {'name': 'Jayant', 'email': 'Jayant@example.com'}The __init__ method allows us to initialize the object in a single step. We create the instance and populate its attributes at the same time.
Compare that to doing it manually, in two steps:
class User:
def setup(self, name, email):
self.name = name
self.email = email
user = User()
user.setup("Jayant", "Jayant@example.com")
print(user.__dict__)
# {'name': 'Jayant', 'email': 'Jayant@example.com'}Functionally, this gives us the same result. But Python gives us __init__ to make this process automatic and consistent. As soon as we instantiate the object, Python calls __init__ for us with the arguments we provide.
This is what allows us to create fully-formed objects in one line — and what makes object-oriented design in Python more predictable and readable. There is a lesser-used part of the instantiation process: __new__.
Quick Note on __new__
So far we’ve been talking about __init__, which handles object initialization. But before Python ever calls __init__, it first has to create the object — and that’s where __new__ comes in.
Here’s how the full flow looks:
- Python calls
__new__to create the new object. - Then it calls
__init__to initialize that object.
By default, we don’t need to define __new__ ourselves. It’s implemented by the base object class, and it usually just returns a new, empty instance.
We typically only override __new__ in special cases — like when working with immutable types (e.g., tuple, str) or customizing object creation in frameworks or metaclasses.
For example, this is valid but rarely needed:
class User:
def __new__(cls, *args, **kwargs):
print("Creating a new User instance")
return super().__new__(cls)
def __init__(self, name):
print("Initializing User")
self.name = name
user = User("Jayant")
# Creating a new User instance
# Initializing UserUnless we have a reason to intercept object creation itself, we stick to __init__. That’s where almost all of the practical, initialization happens in Python classes.
What Python Really Does When You Create an Object
When we call something like User("Jayant", "Jayant@example.com"), Python is doing a bit more than it lets on.
Under the hood:
- It creates a new object — that’s the role of
__new__. - It immediately initializes that object — that’s where
__init__comes in.
And __init__ isn’t just a setup hook. It’s part of Python’s core protocol for object-oriented design. When we define it, we’re saying: “Every time someone creates an instance of this class, here’s how it should be initialized.”
What makes this predictable is that Python applies the same pattern across the board — whether we’re creating a user-defined object or something built-in like a list or str. The methods may differ, but the life cycle is the same: first creation, then initialization.
That’s why writing classes in Python often starts with __init__ — not because it’s required, but because it’s where the behavior of each instance begins.






