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

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

createsruns oncompletes beforeMyClass(some_value)instantiation callNew object__init__initializes stateObjectreturned to your code
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

__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

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

Initializing a Profile

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

Instantiation argument: The string "Ari" is supplied when Profile is instantiated.

__init__ parameter: The supplied value is received by the parameter named username.

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

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

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

__init__ initializescontainsObjectbefore __init__ completesObjectafter __init__ completesusername"Ari"
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

  • Using the wrong method name

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

    Fix: Use def __init__(self, username):

  • Leaving out self as the first parameter

    The __init__ method must include self as its first parameter.

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

  • Failing to use self when binding a parameter to object state

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

    Fix: Bind the value as self.username = username.

  • Expecting to initialize each object manually after creation

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

    Fix: Put the initial-state setup inside __init__.

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.

Practice Answer

Trace Account("Mina") through __init__.

Argument: "Mina" is supplied during instantiation.

Parameter: The value is received by owner.

Attribute: self.owner is assigned the value received by owner.

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

Key Takeaways

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

Key Takeaways

  • __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.