Last modified: Nov 26, 2026
Python Class __init__ Function
The __init__ method is one of the most important parts of any Python class. It runs automatically when you create a new object.
Beginners often call it the "constructor." That is close, but not fully correct. In Python, __init__ is an initializer. It sets up an object after Python has already created it.
This guide explains what __init__ does, how self works, common mistakes, and best practices. Every section includes short examples with output.
What Is the __init__ Function?
__init__ is a special method. Python calls it every time you build a new instance of a class.
Its job is to give the object its starting state. That means setting attributes like a name, age, or price.
class Dog:
def __init__(self, name):
# This runs when a Dog object is created
self.name = name
my_dog = Dog("Rex")
print(my_dog.name)
Rex
Notice that we never call __init__ directly. Python does it for us when we write Dog("Rex").
How the self Parameter Works
The first parameter of __init__ is almost always named self. It refers to the object being created.
When you write self.name = name, you attach the value to that specific object. Other objects keep their own values.
class Dog:
def __init__(self, name):
self.name = name
dog1 = Dog("Rex")
dog2 = Dog("Bella")
print(dog1.name)
print(dog2.name)
Rex
Bella
Each object stores its own data. self is the link between the method and the instance. If you forget it, Python raises a TypeError.
Default Values and Optional Arguments
You can give parameters default values. This makes some arguments optional when creating objects.
class User:
def __init__(self, name, role="member"):
self.name = name
self.role = role
user1 = User("Ana")
user2 = User("Sam", "admin")
print(user1.role)
print(user2.role)
member
admin
Default values keep your class flexible. Just avoid using mutable defaults like lists or dictionaries.
Mutable defaults are shared between objects. This causes bugs that are hard to find. Use None and create the list inside the method instead.
class Team:
def __init__(self, members=None):
# Safe way to handle a list default
self.members = members if members is not None else []
team_a = Team()
team_b = Team()
team_a.members.append("Ana")
print(team_a.members)
print(team_b.members)
['Ana']
[]
Common Mistakes with __init__
One common error is forgetting self in the parameter list. Another is returning a value from __init__.
__init__ must return None. If you return anything else, Python raises a TypeError.
class Bad:
def __init__(self):
return 5 # This will fail
obj = Bad()
TypeError: __init__() should return None, not 'int'
A third mistake is creating objects without the required arguments. If __init__ needs a name, you must pass one.
Not every class needs an __init__. If you want to know what happens when you skip it, read this guide on a Python class without __init__ explained step by step.
__init__ vs __new__
Python actually uses two steps to create an object. First, __new__ creates the instance. Then, __init__ initializes it.
Most developers only need __init__. You touch __new__ only for special cases like immutable types.
For a deeper walkthrough of the initializer, check this Python class __init__ method guide with more examples.
Using __init__ with Inheritance
When a child class has its own __init__, it overrides the parent version. You can call the parent method with super().
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name, color):
# Call the parent initializer
super().__init__(name)
self.color = color
cat = Cat("Milo", "gray")
print(cat.name, cat.color)
Milo gray
Always call super().__init__() when you extend a parent class. Skipping it can leave parent attributes unset.
__init__ and Class Decorators
Decorators can wrap or modify classes. They run after the class body is built, which affects how __init__ behaves.
If you plan to decorate a class, this Python class decorator guide explains the order of execution clearly.
Practical Example: A Bank Account Class
Let's put everything together. This class uses defaults, validation, and a clear initializer.
class BankAccount:
def __init__(self, owner, balance=0):
# Store the owner name
self.owner = owner
# Prevent negative starting balance
if balance < 0:
raise ValueError("Balance cannot be negative")
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
account = BankAccount("Ana", 100)
print(account.owner)
print(account.deposit(50))
Ana
150
This example shows how __init__ sets a safe starting state. The validation runs before any other method can be used.
Best Practices for __init__
Keep __init__ short and focused. It should only set attributes, not run heavy logic.
Use clear parameter names. Avoid too many arguments. If a class needs many inputs, group them in a config object.
Add type hints for readability. Validate inputs early so bugs appear fast.
Do not call methods that depend on a fully built object inside __init__. The object may not be ready yet.
Conclusion
The __init__ function gives every Python object its starting values. It runs automatically when you create an instance.
Remember three things. Always include self. Never return a value. And call super().__init__() when using inheritance.
With these rules and the examples above, you can write clean classes with confidence. Practice with small classes first, then build bigger ones.