Last modified: Nov 26, 2026
Python init: What It Does
If you have written a Python class, you have seen the __init__ method. It looks strange at first. But it is one of the most important parts of object-oriented Python.
This article explains what __init__ is, when it runs, and how to use it well. We will also cover common mistakes and best practices. Every example includes code and output.
What Is init in Python?
__init__ is a special method in Python. Python calls it automatically when you create a new object from a class.
People often call it the constructor. That is close, but not exact. The real constructor is __new__. The __init__ method initializes the object after it exists.
Its job is simple: set up the object. It gives attributes their starting values. It prepares the object for use.
How init Runs
When you write MyClass(), Python does two things. First, it creates the object. Then, it calls __init__ on that object.
You never call __init__ yourself. Python calls it for you. This is why it runs the moment you create an instance.
class Dog:
def __init__(self, name):
# This runs when a Dog object is created
self.name = name
print(f"A dog named {self.name} was created")
my_dog = Dog("Rex")
A dog named Rex was created
The print statement runs without you calling it. That shows __init__ fires automatically. If you are new to this, the Python __init__ beginner's guide walks through the basics step by step.
The self Parameter
Every instance method takes self as its first parameter. __init__ is no different.
self refers to the object being created. When you write self.name = name, you store data on that specific object.
Each object gets its own attributes. Two dogs can have different names. They share the class but not the values.
class Dog:
def __init__(self, name):
self.name = name
dog_one = Dog("Rex")
dog_two = Dog("Bella")
print(dog_one.name)
print(dog_two.name)
Rex
Bella
Notice that self.name holds a different value for each object. That is the whole point of instance attributes.
Default Values in init
You can give parameters default values. This makes them optional when you create an object.
Defaults keep your class flexible. Users can pass a value or skip it.
class Dog:
def __init__(self, name, age=1):
self.name = name
self.age = age
puppy = Dog("Max")
older = Dog("Rex", 5)
print(puppy.age)
print(older.age)
1
5
Be careful with mutable defaults like lists or dicts. They are shared across all instances. Use None as the default and create the list inside __init__ instead.
init vs __new__
Some learners confuse __init__ with __new__. They play different roles.
__new__ creates the object. It returns a new instance. __init__ then sets it up.
In most code, you only write __init__. You rarely need __new__. It matters for immutable types like int or tuple.
The Python class __init__ function article explains this relationship in more depth.
Inheritance and init
When a class inherits from another, the child can call the parent's __init__. This avoids repeating setup code.
Use super() to call the parent method. It is the modern and clean way to do it.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call the parent __init__
self.breed = breed
my_dog = Dog("Rex", "Labrador")
print(my_dog.name)
print(my_dog.breed)
Rex
Labrador
If you forget to call super().__init__(), the parent setup never runs. That often causes missing attributes later. The Python class __init__ method guide covers inheritance patterns well.
Classes Without init
Not every class needs __init__. If your class only holds methods and no instance data, you can skip it.
Python uses a default __init__ that does nothing. Your class still works fine.
class Greeter:
def greet(self):
return "Hello!"
g = Greeter()
print(g.greet())
Hello!
For more on this topic, read about a Python class without __init__ explained in detail.
Common Mistakes
Forgetting self. Every instance method needs self as the first parameter. Leaving it out causes a TypeError.
Using mutable defaults. A default list is shared by every instance. This creates hard-to-find bugs.
Returning a value.__init__ must return None. Returning anything else raises a TypeError.
Skipping super(). In subclasses, always call the parent __init__ when needed.
Doing too much work. Keep __init__ light. Avoid heavy file reads, network calls, or complex logic. It should only set up the object.
Best Practices
Use clear parameter names. Match them to attribute names for readability.
Validate inputs early. Raise a ValueError if data is invalid.
Keep the method short. If it grows past a screen, split logic into helper methods.
Document your parameters. A short docstring helps future readers.
class BankAccount:
def __init__(self, owner, balance=0):
# Validate the opening balance
if balance < 0:
raise ValueError("Balance cannot be negative")
self.owner = owner
self.balance = balance
account = BankAccount("Anna", 100)
print(account.owner, account.balance)
try:
bad = BankAccount("Tom", -50)
except ValueError as e:
print(e)
Anna 100
Balance cannot be negative
This example shows clean validation. The object never exists in a bad state.
init and Dataclasses
Python 3.7 added dataclasses. They generate __init__ for you. This saves time and reduces boilerplate.
You just declare fields. The decorator writes the setup code.
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
p = Point(3, 4)
print(p.x, p.y)
3 4
Dataclasses are a great choice for simple data containers. For custom logic, write your own __init__.
Conclusion
__init__ is the setup method of every Python object. It runs automatically when you create an instance. It assigns attributes and prepares the object for use.
Remember the key rules. Always include self. Avoid mutable defaults. Call super() in subclasses. Keep the method short and focused.
Mastering __init__ unlocks clean, readable class design. Start with simple examples, then add validation and inheritance as your skills grow.