Last modified: Nov 26, 2026

Python Class __init__ Method Guide

The __init__ method is one of the first concepts every Python beginner meets when learning classes. It runs automatically when you create an object. It sets up the object's starting state.

This guide explains what the __init__ method does. It also shows how to use it well with short examples and clear output.

What Is the __init__ Method?

In Python, a class is a blueprint for creating objects. The __init__ method is a special method inside that class. It is called when a new instance is created.

Many people call it the constructor. That is close enough for beginners. Technically, Python creates the object first. Then it calls __init__ to initialize it.

The method receives the new object as its first argument. By convention, that argument is named self. You use self to store data on the object.

Basic Example of __init__

Here is a simple class with an __init__ method. It stores a name and an age on each new object.


class Person:
    def __init__(self, name, age):
        # Store values on the new object
        self.name = name
        self.age = age

# Create an object; __init__ runs automatically
p = Person("Alice", 30)

print(p.name)
print(p.age)
  

Alice
30
  

Notice that you never call __init__ directly. You call the class, and Python handles the rest.

Why self Matters

The self parameter refers to the specific object being created. Each object gets its own values. That is why two objects from the same class can hold different data.


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p1 = Person("Alice", 30)
p2 = Person("Bob", 25)

print(p1.name, p1.age)
print(p2.name, p2.age)
  

Alice 30
Bob 25
  

If you forget self, Python raises a TypeError. The method will not know which object to work with.

Default Arguments in __init__

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


class Person:
    def __init__(self, name, age=18):
        self.name = name
        self.age = age

p1 = Person("Alice")
p2 = Person("Bob", 25)

print(p1.name, p1.age)
print(p2.name, p2.age)
  

Alice 18
Bob 25
  

Use defaults with care. Mutable defaults like lists can cause shared-data bugs. Prefer None and create the object inside the method when needed.

__init__ and Inheritance

When a class inherits from another class, the child class often needs to call the parent's __init__. The built-in super function helps here.


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

class Dog(Animal):
    def __init__(self, name, breed):
        # Call parent __init__ to set name
        super().__init__(name)
        self.breed = breed

d = Dog("Rex", "Labrador")
print(d.name, d.breed)
  

Rex Labrador
  

If you want a deeper look at inheritance calls, read this guide on the Python super() Function Explained. It covers common patterns and mistakes.

Common Mistakes with __init__

Beginners often call __init__ directly. That is usually wrong. Create objects by calling the class name instead.

Another common mistake is forgetting to store arguments on self. If you do not assign them, the data disappears after the method ends.


class BadPerson:
    def __init__(self, name):
        # Forgot self.name = name
        pass

b = BadPerson("Alice")
print(b.name)  # This will fail
  

AttributeError: 'BadPerson' object has no attribute 'name'
  

Always check that every argument you need later is saved on self.

__init__ vs __new__

Python also has a __new__ method. It creates the object. Then __init__ initializes it.

For most beginner projects, you only need __init__. Use __new__ only for advanced cases like immutable types or custom object creation.

Using __init__ with Properties

You can combine __init__ with properties to control access to attributes. This helps when you need validation or computed values.


class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius

    @property
    def fahrenheit(self):
        return self.celsius * 9 / 5 + 32

t = Temperature(25)
print(t.fahrenheit)
  

77.0
  

For a full explanation, see the Python Property Decorator Explained. It shows how to add getters and setters cleanly.

Class Decorators and __init__

Class decorators can wrap or modify classes. They are useful for logging, registration, and adding behavior. They do not replace __init__, but they can change how a class is used.

If you want to understand how decorators interact with classes, read the Python Class Decorator Guide. It walks through practical examples step by step.

Clean __init__ Design Tips

Keep __init__ short. It should set up the object, not do heavy work. Move complex logic into separate methods.

Use clear parameter names. Avoid too many arguments. If a class needs many settings, consider grouping them in a dictionary or a small config object.

Validate inputs when it matters. Raise a ValueError early so bugs are caught fast.


class Account:
    def __init__(self, balance):
        if balance < 0:
            raise ValueError("Balance cannot be negative")
        self.balance = balance

a = Account(100)
print(a.balance)
  

100
  

This small check prevents invalid objects from being created. It makes your code safer and easier to debug.

Conclusion

The __init__ method is the standard way to set up a new Python object. It runs automatically, receives self, and stores data on the instance.

Start with simple classes and clear assignments. Add defaults only when they make sense. Use super for inheritance. Keep the method short and focused.

Once you are comfortable with __init__, you can explore related topics like properties, decorators, and inheritance. Each one builds on the same object model and makes your Python classes more powerful.