Concepts / The self Parameter Explained

The self Parameter Explained

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

  • Programming

From Class Call to Initialized Object

When you write MyClass(some_value), Python does more than simply produce an object. Python creates a new object and automatically calls the special method named __init__ on that object. The initialization method runs before the object is returned to your code, so the object can emerge with its initial state already set up.

createsautomatically invokescompletes before returnPerson('Alex')class callPerson objectnew object__init__runs automaticallypinitialized Person
What happens first when an object is created, and when does __init__ run relative to the new object becoming initialized?

You do not manually call __init__ when creating an object. Every instantiation automatically triggers it, providing a consistent setup process.

The Initializer Method

__init__ is a special method used to initialize an object's state immediately. Its name has two underscores at the beginning and two underscores at the end. This distinctive naming convention signals that Python should recognize the method and call it automatically at the appropriate point in an object's lifecycle.

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

python

The first parameter is self. After self, the method declares the parameters needed for initialization. In this example, name is the initialization parameter. When an instance is created with a name argument, Python supplies the object as self and supplies the creation argument as name. The caller passes the initialization argument, but does not pass self; Python handles self automatically.

Following Alex into Object State

Tracing Person('Alex')

Trace what happens when a Person object is created with the argument 'Alex'.

Instantiation argument: The class is called as Person('Alex'). The string 'Alex' is the value supplied for the initialization data.

__init__ parameter: Python automatically calls __init__ on the newly created Person object. Inside that call, self represents that particular object, and name receives 'Alex'.

Attribute assignment: The statement self.name = name stores the value from the name parameter as the object's name instance attribute.

Returned object: After __init__ finishes, the object is returned to the code with its name attribute already set. A variable such as p can then hold a reference to that Person instance.

The value 'Alex' travels from the instantiation call to the name parameter and then into the object's self.name attribute.

becomesassigned by self.name = nameowns'Alex'instantiation argumentname__init__ parameterPerson objectselfself.name'Alex'
How do values passed when creating an object move into __init__ parameters and become instance attributes?

The two uses of name in self.name = name have different roles. The name on the right is the parameter receiving the value from instantiation. The name after self. identifies the instance attribute that stores that value. self connects the assignment to the particular object currently being initialized.

python

Before and After Initialization

At the instant Python creates a Person object, the source describes it as existing without attributes. The __init__ method then assigns the name attribute. After initialization completes, the same object has name set to 'Alex'. This state change happens automatically as part of instantiation.

has attributePerson objectno attributesPerson objectinitialized statenamenot yet setname'Alex'
What changes in an object's stored state after __init__ assigns a value to its instance attribute?

Multiple Values in One Setup

__init__ can receive several initialization parameters. For example, when Student('Jordan', 12345, 10) is used, Python runs __init__ with those three arguments. The object can then emerge with all corresponding attributes populated, rather than requiring separate assignments after creation.

python

Here, self still represents the particular Student object. name, student_id, and grade receive the values supplied during instantiation. Each assignment beginning with self. places one of those values into the object's state.

Treat the __init__ parameter list as a clear description of the data an object needs. A definition such as __init__(self, name, age) communicates that a Person requires a name and an age for initialization.

Mistakes with self and __init__

  • Leaving out self as the first parameter

    The source specifies that __init__ must include self as its first parameter. self represents the object being initialized.

    Fix: Use def __init__(self, name): and pass only the initialization value when creating the object.

  • Using the wrong method name

    Python recognizes the initializer through the distinctive name with double underscores on both sides.

    Fix: Write the method name exactly as __init__.

  • Assuming the caller passes self

    Python handles self automatically when it invokes __init__.

    Fix: Create the object with the initialization value, such as Person('Alex').

  • Forgetting to bind a parameter to an instance attribute

    The source identifies self.name = name as the assignment that stores the value as part of the object's state.

    Fix: Use self.name = name when the value should become an attribute of that object.

Practice the Data Trace

EASY

Trace the value 10 through this instantiation: Student('Jordan', 12345, 10). Identify which __init__ parameter receives 10 and which instance attribute stores it.

Hints
  • Match the third instantiation argument with the third initialization parameter.
  • The stored attribute uses self. followed by that parameter's name.

What do you think happens?

Before reading the answer, predict what the object has after Student('Jordan', 12345, 10) finishes running __init__.

  • Only the name attribute
  • The name, student_id, and grade attributes
  • No attributes until they are assigned manually
Reveal answer

Answer: The name, student_id, and grade attributes

The source explains that __init__ receives all three initialization values and stores each one as an instance attribute before the object is returned.

Key Takeaways

  1. __init__ runs automatically during object creation, before the initialized object is returned to your code.
  2. The method name has double underscores on both sides: __init__.
  3. self is the first parameter and represents the particular object being initialized; Python supplies it automatically.
  4. Arguments passed during instantiation become __init__ parameter values, and assignments such as self.name = name store them as instance attributes.
  5. Defining __init__ makes initialization consistent and documents the data an object needs.

Key Takeaways

  • __init__ is automatically invoked when an object is created.
  • The method must use the exact double-underscore name __init__ and include self first.
  • self identifies the particular object whose state is being initialized.
  • Instantiation arguments flow into __init__ parameters and can be stored as instance attributes.
  • Automatic initialization gives every instance a consistent and predictable starting state.