Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

__init__

__init__ is a special method that initializes a new instance when its class is called. Arguments supplied during instantiation are passed to it, allowing each object to start with customized state.

How it works

Define __init__ on a class to customize each newly created instance. Python invokes it automatically during class instantiation, passing along the arguments supplied to the class call; the method can then assign instance attributes such as self.name.

Syntax

def __init__(self, ...):

Parameters

NameTypeDescription
selfobjectThe instance being initialized.
*argsobjectOptional application-specific values supplied when the class is called; the page illustrates a name argument.

Returns

The documentation page describes initialization during instantiation but does not specify a separate return value for __init__().

Key points

  • Calling a class first creates an object; __init__ is the class-defined hook used to establish its initial state.
  • Use __init__ when every instance should begin with deliberate, usable state rather than an unconfigured object.
  • The identifier is a double-underscore special method name, written exactly as __init__.
  • The basic declaration accepts self, which refers to the instance being initialized.
  • Add parameters when callers need to provide values that determine the instance's initial configuration.

Common mistakes

  • The method runs automatically when the class is called, so it should not normally be invoked separately for ordinary construction.

Example: Initialize instance state

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

user = User("Ada")
print(user.name)

The constructor receives the argument supplied when the class is instantiated and stores it on the new instance.

When to use it

Establish initial state

Set default attributes such as lists, counters, or configuration values when each object is created.

Customize instances

Accept constructor arguments so callers can create instances with different values.

Configure object behavior

Prepare instance variables used by the object's later methods.

Interview questions

QWhat is __init__ used for?

__init__ is a special method that initializes a newly created instance. Python calls it automatically when the class is instantiated, and arguments passed to the class are forwarded to it.

Follow-up: What happens if __init__ accepts name but the class is called without it? The call raises a missing-argument TypeError.

QHow do constructor arguments affect an instance?

Parameters declared by __init__ let callers customize initial state. The example stores the supplied value on the instance through an assignment such as self.name = name.

Follow-up: How can multiple instances retain different values? Assign the constructor argument to each instance's attributes; those attributes are separate per object.

QCan __init__ take arguments?

A class can define __init__ with no arguments beyond self, or with additional parameters for flexible initialization. Calling the class passes those additional values to the method.

Follow-up: What should a class do when initialization needs optional data? Give the parameter a default value or accept an appropriate optional argument and initialize the instance consistently.

  • class instantiation
  • instance variables
  • class variables
  • special methods

Source: 9. Classes — Python 3.15.0 documentation. Checked 2026-10-16.