Concepts / The self Parameter Explained

The self Parameter Explained

The left side of self.name = name identifies where the value is stored: the particular object represented by self.

  • 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…

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.

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

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.

Definition

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

Worked example

Tracing Person('Alex')

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

  1. 1
    Instantiation argument

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

  2. 2
    __init__ parameter

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

  3. 3
    Attribute assignment

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

  4. 4
    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.

Result

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

'Alex'instantiation argumentname__init__ parameterPerson objectselfself.name'Alex'becomesassigned by self.name = nameowns
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.

Person objectno attributesPerson objectinitialized statenamenot yet setname'Alex'has attribute
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.

Best practice

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__

  • Common mistake

    Leaving out self as the first parameter

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

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

    Do this instead

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

  • Common mistake

    Using the wrong method name

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

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

    Do this instead

    Write the method name exactly as __init__.

  • Common mistake

    Assuming the caller passes self

    What it looks like
    Person(object, 'Alex')
    Why it fails

    Python handles self automatically when it invokes __init__.

    Do this instead

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

  • Common mistake

    Forgetting to bind a parameter to an instance attribute

    What it looks like
    def __init__(self, name):
        name = name
    Why it fails

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

    Do this instead

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

At a glance

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

More concepts starting with T