Concepts / The __init__ Method

The __init__ Method

The __init__ method is a special method that runs automatically when you create an object, allowing you to initialize its state immediately.

  • Programming
Interactive lab

Try it: Classes, Instances and Attributes

How a class creates instances with __init__ and self, why an attribute read checks the instance's __dict__ before the class, why assigning through an instance never changes the class, how a class-level list is shared by every instance, and why obj.method(x) is Class.method(obj, x).

How it works

  1. Dog(...) makes a new, empty instance and calls Dog.__init__(new_object, ...); self.name = ... stores into that object's __dict__.
  2. Reading obj.attr looks in obj.__dict__ first, then in the class.
  3. Assigning obj.attr = value always writes obj.__dict__, so it shadows a class attribute for that one object only.
  4. Mutating a class-level list (self.tricks.append) changes the one list every instance shares.
  5. obj.method is found on the class and bound to obj, so obj.method(x) runs Class.method(obj, x).

Default run (30 steps): Ready: class Dog and the statements you picked. Watch where each attribute is found and where each assignment writes. … Finished. Printed: ['sit'] / Wolf / Canis familiaris / Fido is 5.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. Object numbers are illustrative, not real id() values.

Educational simulation

Loading the simulation…

When Objects Come to Life

When you create an object, Python automatically runs a special method named __init__. This gives the object its initial state immediately, before your code receives the object back. Instead of creating an object first and then setting its attributes through separate manual steps, you define the setup once inside __init__, so every instance follows the same initialization process.

What do you think happens?

Suppose a class has an __init__ method and you create an object with MyClass(some_value). When does __init__ run?

  • Only when you call it manually later

  • Automatically during object creation, before your code receives the object

  • Only after you change an attribute

  • It never runs automatically

Reveal answer
Answer:

Automatically during object creation, before your code receives the object

Every instantiation automatically triggers __init__. The method runs on the newly created object before the surrounding code gets that object back.

The Creation Sequence

MyClass(some_value)instantiation callNew object__init__initializes stateObjectreturned to your codecreatesruns oncompletes before
What happens first when an object is created, and when does __init__ execute?

The important order is instantiation, automatic execution of __init__, and then return of the initialized object to your code. The call MyClass(some_value) therefore does more than identify a class. It also starts the initialization process for the new object.

Naming and Parameters

Definition

__init__ is a special method whose name has two underscores before init and two underscores after it. Its first parameter must be self. Additional parameters can receive the values supplied when an object is instantiated.

python

In this generated example, __init__ has the required self parameter followed by username, an initialization parameter. The assignment uses self to bind the received username to an instance attribute named username. That binding makes the value part of the object's state.

From Arguments to State

"Ari"instantiation argumentusername__init__ parameterself.usernameinstance attributeObject stateusername = "Ari"suppliesbinds through selfbecomes part of
How does a value passed during instantiation move into an __init__ parameter and become an instance attribute?
Worked example

Initializing a Profile

Trace the value in the generated expression Profile("Ari") through the __init__ method.

  1. 1
    Instantiation argument

    The string "Ari" is supplied when Profile is instantiated.

  2. 2
    __init__ parameter

    The supplied value is received by the parameter named username.

  3. 3
    Attribute binding

    Inside __init__, self.username = username uses self to bind the parameter value to the object's username attribute.

  4. 4
    Initialized state

    The resulting object has username set to "Ari" as part of its initial state.

Result

The value travels from the instantiation argument to username and then to self.username.

python
Output
Ari

The assignment inside __init__ is the step that transfers the parameter value into object state. self refers to the particular instance being initialized, while username on the right side represents the value received by the parameter. The username on the left side identifies the attribute stored on that instance.

State Before and After

Objectbefore __init__ completesObjectafter __init__ completesusername"Ari"__init__ initializescontains
How does the object move from an uninitialized view of its state to state containing values assigned by __init__?

The diagram shows the role of __init__ as an initialization step: it sets up the object's initial state. After the method binds a value through self, that value becomes an instance attribute and is available as part of the object. Because the method runs automatically for every instantiation, each instance receives the initialization process without a separate manual setup step.

Mistakes with Initialization

  • Common mistake

    Using the wrong method name

    What it looks like
    def init(self, username):
    Why it fails

    Python recognizes the special automatic behavior through the exact name __init__, including both pairs of underscores.

    Do this instead
    Use def __init__(self, username):
  • Common mistake

    Leaving out self as the first parameter

    What it looks like
    def __init__(username):
    Why it fails

    The __init__ method must include self as its first parameter.

    Do this instead

    Place self first, followed by any initialization parameters you need.

  • Common mistake

    Failing to use self when binding a parameter to object state

    What it looks like
    username = username
    Why it fails

    The source method for making a parameter part of the object's state is to use self to bind it to an instance attribute.

    Do this instead

    Bind the value as self.username = username.

  • Common mistake

    Expecting to initialize each object manually after creation

    What it looks like

    Create an object first, then treat initialization as a separate required setup process.

    Why it fails

    Every instantiation automatically triggers __init__, so initialization is defined in the class and runs as part of object creation.

    Do this instead

    Put the initial-state setup inside __init__.

Best practice

Define the object's initial-state setup in __init__ rather than repeating separate setup steps for each instance. This keeps initialization consistent and makes the path from instantiation arguments to instance attributes explicit.

Trace It Yourself

EASY

Consider the generated class declaration: class Account: def __init__(self, owner): self.owner = owner. For the instantiation Account("Mina"), identify the instantiation argument, the __init__ parameter, and the resulting instance attribute value.

Hints
  • Find the value supplied inside Account(...).
  • Match that value to the parameter after self.
  • Follow the assignment that begins with self.
Worked example

Practice Answer

Trace Account("Mina") through __init__.

  1. 1
    Argument

    "Mina" is supplied during instantiation.

  2. 2
    Parameter

    The value is received by owner.

  3. 3
    Attribute

    self.owner is assigned the value received by owner.

Result

The object's owner attribute is initialized with "Mina".

Key Takeaways

  1. 1
    __init__ is a special method that runs automatically when an object is created.
  2. 2
    The method name uses double underscores on both sides, and self must be its first parameter.
  3. 3
    Values supplied during instantiation can be received by __init__ parameters.
  4. 4
    Using self to bind those parameters creates instance attributes and establishes the object's initial state.
  5. 5
    Every instantiation triggers the initialization process, promoting consistent object setup.

At a glance

  • __init__ runs automatically during object creation, before the object is returned to your code.
  • Its exact name includes double underscores at the beginning and end.
  • self must be the first parameter, followed by parameters for values needed during initialization.
  • Assignments such as self.attribute = parameter use self to place values into instance state.
  • The data path is instantiation argument, __init__ parameter, and then instance attribute.

Related concepts

More concepts starting with T