Last modified: Nov 26, 2026
Python __init__: A Beginner's Guide
If you are learning Python classes, you will meet __init__ very early. It looks strange at first. But it is one of the most important methods in Python object-oriented programming.
This guide explains what __init__ is, when it runs, and how to use it well. It also covers common mistakes and edge cases. By the end, you will know exactly how to write clean classes with __init__.
What Is __init__ in Python?
__init__ is a special method in Python. It is often called the constructor. It runs automatically when you create a new object from a class.
Its job is to set up the object. That means giving it the initial values it needs to work.
The name has double underscores on both sides. These are called dunder methods, short for double underscore. Python uses many dunder methods to define built-in behavior.
You can read more about how it fits into class design in this Python Class __init__ Function overview.
How __init__ Works
When you call a class, Python creates a new object. Then it calls __init__ on that object. The first parameter is always self.
self refers to the object being created. You use it to attach data to the object.
Here is a simple example:
class Dog:
def __init__(self, name, age):
# Runs automatically when a Dog object is created
self.name = name
self.age = age
my_dog = Dog("Rex", 3)
print(my_dog.name)
print(my_dog.age)
Rex
3
Notice that you never call __init__ yourself. Python does it for you when you write Dog("Rex", 3).
The arguments you pass to the class go straight into __init__.
__init__ vs __new__
Many beginners think __init__ creates the object. It does not. Python uses __new__ to create the object first.
__new__ returns a new instance. Then __init__ is called to initialize that instance.
In most code, you only need __init__. You use __new__ only in rare cases, like when subclassing immutable types such as int or tuple.
This distinction matters because it explains why __init__ must return None. If you return anything else, Python raises a TypeError.
Why __init__ Must Return None
Python expects __init__ to change the object in place. It does not expect a return value.
If you return something, you get an error. Here is what that looks like:
class Bad:
def __init__(self):
return 42 # This is not allowed
x = Bad()
TypeError: __init__() should return None, not 'int'
The fix is simple. Remove the return statement, or use a plain return with no value.
Default Arguments in __init__
You can give parameters default values. This makes objects easier to create.
class User:
def __init__(self, username, role="member"):
self.username = username
self.role = role
u1 = User("ana")
u2 = User("sam", "admin")
print(u1.role)
print(u2.role)
member
admin
But be careful with mutable defaults. Lists and dictionaries are shared between calls if used as defaults.
This causes bugs that are hard to find. Use None as the default and create the object inside the method instead.
class Cart:
def __init__(self, items=None):
# Safe: a new list is created for each Cart
self.items = items if items is not None else []
c1 = Cart()
c2 = Cart()
c1.items.append("apple")
print(c1.items)
print(c2.items)
['apple']
[]
This is one of the most common mistakes in Python. Always watch out for it. For deeper detail on parameters and signatures, see this Python Class __init__ Method Guide.
Inheritance and __init__
When a class inherits from another class, the child class may need to call the parent's __init__. You do this with super().
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name, indoor):
super().__init__(name) # Call the parent's __init__
self.indoor = indoor
c = Cat("Milo", True)
print(c.name, c.indoor)
Milo True
If you forget super(), the parent setup never runs. The object may be missing attributes. That leads to AttributeError later, often far from the real cause.
Using super() is the modern and correct way. It also works well with multiple inheritance.
Classes Without __init__
Not every class needs __init__. If there is nothing to set up, you can skip it.
Python still creates objects fine. You can add attributes later if needed.
If you want to understand when skipping is fine, read Python Class Without __init__ Explained. It covers the details clearly.
Common Mistakes to Avoid
Here are the errors beginners hit most often:
Forgetting self: The first parameter must be self. If you leave it out, Python passes the instance anyway and you get a confusing error.
Calling __init__ directly: Do not call obj.__init__() to reset an object. Create a new object instead.
Using mutable defaults: As shown above, this causes shared state between objects.
Returning a value: __init__ must return None.
Forgetting super(): The parent class setup will not run.
__init__ and Decorators
Sometimes you want to wrap setup logic or validate inputs. Decorators can help. You can learn the basics in this Python Class Decorator Guide.
For example, you can check that an age is positive before storing it. Keep the logic small and readable.
Do not overload __init__ with heavy work. Doing database calls or file reads inside it makes objects slow and hard to test. Prefer a separate method for that.
Best Practices for __init__
Keep the method short. Set attributes and do light validation only.
Use clear parameter names. Give sensible defaults. Avoid mutable defaults.
Always call super().__init__() in subclasses. Document what each attribute means.
If setup gets complex, split it into helper methods. Your code will be easier to read and test.
Conclusion
__init__ is the method that initializes new objects in Python. It runs automatically when you create an instance. It sets up attributes and prepares the object for use.
Remember the key rules. It must return None. It needs self as the first parameter. Avoid mutable defaults. Call super() in subclasses.
With these basics, you can write clean, reliable classes. Practice with small examples, and __init__ will quickly feel natural.