Last modified: Nov 26, 2026

Python __init__ Method Explained

The __init__ method is one of the first things you meet when you start writing Python classes. It looks strange at first. Yet it plays a central role in object-oriented programming.

This guide explains what __init__ does, when it runs, and how to use it well. It also covers common mistakes and best practices.

What Is the __init__ Method?

The __init__ method is a special method in Python. It runs automatically when you create a new object from a class.

People often call it the constructor. That name is close, but not exact. In Python, the real object is created by __new__. The __init__ method then sets up the object's initial state.

If you want a deeper look at what this method does, read this explanation of what Python init does.

A Simple Example

Here is a small class with an __init__ method.


class Dog:
    def __init__(self, name, age):
        self.name = name  # store the dog's name
        self.age = age    # store the dog's age

my_dog = Dog("Rex", 3)
print(my_dog.name)
print(my_dog.age)

Rex
3

When you write Dog("Rex", 3), Python creates a new object. Then it calls __init__ with that object plus your arguments.

The self parameter refers to the new object. It is passed automatically. You never pass it yourself.

Why Is It Called __init__?

The name means "initialize". The method's job is to give the new object the data it needs to work.

Without it, an object would start empty. You would have to set attributes by hand after every creation.

The __init__ method keeps setup in one place. That makes your code cleaner and less error-prone.

How __init__ Fits With def

You define __init__ using the def keyword, just like any other method. If you are new to functions, this guide on what Python def means and how it works will help.

The pattern is simple. Write def __init__(self, ...) inside your class body. Then assign values to self.

Remember that self must be the first parameter. Python passes the instance there automatically.

Default Values and Optional Arguments

You can give parameters default values. This makes some arguments optional.


class User:
    def __init__(self, username, is_admin=False):
        self.username = username
        self.is_admin = is_admin  # default is False

guest = User("alice")
admin = User("bob", True)

print(guest.is_admin)
print(admin.is_admin)

False
True

Be careful with mutable defaults like lists or dictionaries. They are shared between calls and can cause surprising bugs.

Use None as the default and create the object inside the method instead.


class Team:
    def __init__(self, members=None):
        if members is None:
            members = []  # fresh list for each team
        self.members = members

What Happens Without __init__?

A class does not need an __init__ method. Python will still create objects from it.

In that case, the object starts with no custom attributes. You can add them later, but the setup is not automatic.

To see the difference clearly, read this guide on a Python class without __init__.

Common Mistakes With __init__

Forgetting self. Every instance method needs self as its first parameter. Without it, Python raises a TypeError.

Returning a value. The __init__ method must return None. Returning anything else causes an error.

Confusing it with __new__. Use __init__ for setup. Use __new__ only when you need to control object creation itself.

Doing too much work. Keep __init__ focused on assigning attributes. Heavy logic belongs in separate methods.

Class Variables vs Instance Variables

Attributes set on self belong to each instance. Attributes set on the class are shared.


class Counter:
    total = 0  # class variable, shared

    def __init__(self, name):
        self.name = name  # instance variable, unique

a = Counter("first")
b = Counter("second")

print(a.name, b.name)
print(Counter.total)

first second
0

This distinction matters. Changing a class variable through an instance can create confusing behavior.

Inheritance and __init__

When a child class defines its own __init__, it replaces the parent's version. Call the parent method with super() to keep its setup.


class Animal:
    def __init__(self, name):
        self.name = name

class Cat(Animal):
    def __init__(self, name, color):
        super().__init__(name)  # run parent setup
        self.color = color

cat = Cat("Milo", "gray")
print(cat.name, cat.color)

Milo gray

This pattern avoids repeating code and keeps parent behavior intact.

Inspecting __init__ Signatures

Sometimes you need to know which arguments a class expects. Python's inspect module can help.

You can read the parameters of __init__ at runtime. This is useful for building tools, plugins, or test helpers.

For a full walkthrough, see this guide to inspecting function signatures in Python.

Practical Tips for Clean Code

Keep parameters meaningful. Use clear names like username or max_size.

Validate early. Check inputs in __init__ so bad objects never exist.

Use type hints. They make your class easier to read and safer to use.

Document the arguments. A short docstring saves time for anyone reading your class later.

Conclusion

The __init__ method is the setup step for every Python object. It runs automatically, receives self, and assigns the starting state of your instance.

Use it for simple, clear initialization. Avoid mutable defaults and keep the logic light. Call super().__init__() when you extend a parent class.

Once you understand __init__, classes in Python become much easier to read and write. Practice with small examples, and the pattern will feel natural in no time.