The __init__ Method in Python Explained

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

Beginners use __init__ to give each object its starting data. For example, if you create a Dog object, __init__ can store its name and age right away.

Quick example

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

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

Output:

Max
3

__init__ runs automatically when you create an object. It is commonly used to store starting values on self.

What __init__ is

  • __init__ is a special method used inside a class.
  • It runs automatically when a new object is created.
  • Its main job is to set up the object.
  • Beginners often call it a constructor, but in Python it is the initializer.

If you are new to classes, it helps to first understand Python classes and objects.

Why __init__ is useful

  • It lets each object start with its own data.
  • It helps keep related values inside one object.
  • It makes objects ready to use right after creation.
  • It avoids setting every attribute manually after creating the object.

Without __init__, you would often need to create an object first and then assign values one by one. That works sometimes, but it is less clear and easier to forget.

Basic syntax

You define __init__ inside a class like this:

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

Important parts:

  • def __init__(self, ...): defines the initializer.
  • The first parameter is usually self.
  • self refers to the specific object being created.
  • Extra parameters are values passed in when creating the object.

When you want a clearer review of parameters, see function parameters and arguments in Python.

How object creation works

When you create an object, Python does these steps:

  1. You write a class definition.
  2. You create an object by calling the class like a function.
  3. Python makes the new object.
  4. Python then calls __init__ automatically with that object as self.

Example:

class Car:
    def __init__(self, brand):
        self.brand = brand

my_car = Car("Toyota")
print(my_car.brand)

Output:

Toyota

Even though you wrote Car("Toyota"), Python automatically passes the new object as self.

If you want more practice with this step, see how to create an object in Python.

Setting attributes with self

Inside __init__, you usually save values like this:

self.attribute_name = value

These saved values are called attributes.

Example:

class Book:
    def __init__(self, title, pages):
        self.title = title
        self.pages = pages

my_book = Book("Python Basics", 200)

print(my_book.title)
print(my_book.pages)

Output:

Python Basics
200

Key idea:

  • self.title = title saves the value on the object.
  • self.pages = pages does the same for pages.
  • These attributes can be used later in other methods.

Example with two objects

A big reason to use __init__ is that each object can have its own values.

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

dog1 = Dog("Max", 3)
dog2 = Dog("Bella", 5)

print(dog1.name, dog1.age)
print(dog2.name, dog2.age)

Output:

Max 3
Bella 5

Both objects come from the same class, but each one stores different data.

Parameters in __init__

__init__ can take zero, one, or many extra parameters.

No extra parameters

class Robot:
    def __init__(self):
        self.status = "ready"

r1 = Robot()
print(r1.status)

Output:

ready

One or more required parameters

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

s1 = Student("Ava", 8)
print(s1.name)
print(s1.grade)

If required parameters are missing, Python raises a TypeError.

Default values for optional parameters

class User:
    def __init__(self, username, active=True):
        self.username = username
        self.active = active

user1 = User("sam")
user2 = User("lee", False)

print(user1.username, user1.active)
print(user2.username, user2.active)

Output:

sam True
lee False

If you see an argument error here, this guide may help: fix missing required positional argument errors.

Common beginner confusion

These points cause a lot of mistakes:

  • __init__ is not usually called directly.
  • self is passed automatically when creating an object.
  • __init__ must be spelled exactly with two underscores before and after init.
  • __init__ does not print the object.
  • __init__ does not return the object.

For example, this is normal:

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

my_cat = Cat("Milo")
print(my_cat.name)

You do not usually write something like this:

# Not the normal beginner pattern
my_cat.__init__("Milo")

What __init__ should return

__init__ should not return a normal value.

It should return None automatically. In most cases, you do not write any return statement in __init__.

Correct:

class Game:
    def __init__(self, title):
        self.title = title

Wrong:

class Game:
    def __init__(self, title):
        self.title = title
        return title

The second example causes an error because __init__ is only for setup. It is not meant to produce output.

When not to use __init__

__init__ is useful, but it should stay focused on setting up object state.

Avoid these patterns:

  • Do not put unrelated logic there.
  • Do not make it too long if helper methods would be clearer.
  • Do not use it when you only need class-level shared data.
  • Keep it focused on starting values for the object.

If you want to add behavior to a class, that usually belongs in normal methods. See basic methods in Python classes.

Common mistakes

These are some common causes of problems when using __init__:

  • Misspelling __init__ as _init_ or init
  • Forgetting the self parameter
  • Forgetting to pass required arguments when creating an object
  • Using a local variable instead of self.attribute
  • Trying to return a value from __init__
  • Expecting __init__ to run without creating an object

Example of a common mistake:

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

This is wrong because self is missing from the parameter list.

Correct version:

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

Another common mistake:

class Dog:
    def __init__(self, name):
        name = name

This does not save the value on the object. It only creates a local variable.

Correct version:

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

Debugging tips

If your object is not behaving as expected, these commands can help:

print(type(my_object))
print(my_object.__dict__)
help(ClassName)
dir(my_object)

What they do:

  • print(type(my_object)) shows what class the object belongs to.
  • print(my_object.__dict__) shows the attributes stored on the object.
  • help(ClassName) shows information about the class and its methods.
  • dir(my_object) lists available attributes and methods.

Example:

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

my_dog = Dog("Max", 3)

print(type(my_dog))
print(my_dog.__dict__)

Possible output:

<class '__main__.Dog'>
{'name': 'Max', 'age': 3}

FAQ

Is __init__ required in every class?

No. You only need it when your object needs setup or starting values.

Does __init__ create the object?

No. Python creates the object first, then __init__ initializes it.

Why do we use self in __init__?

self refers to the current object, so you can store values on that specific object.

Can __init__ have default values?

Yes. You can give parameters default values just like in normal functions.

Can a class work without __init__?

Yes. But objects from that class will not get automatic setup unless you add it.

See also

Once you understand __init__, the next good step is building a simple class of your own and creating a few objects with different starting values.