The self Parameter Explained
The left side of self.name = name identifies where the value is stored: the particular object represented by self.
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
- Dog(...) makes a new, empty instance and calls Dog.__init__(new_object, ...); self.name = ... stores into that object's __dict__.
- Reading obj.attr looks in obj.__dict__ first, then in the class.
- Assigning obj.attr = value always writes obj.__dict__, so it shadows a class attribute for that one object only.
- Mutating a class-level list (self.tricks.append) changes the one list every instance shares.
- 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.
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.
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.
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'.
- 1Instantiation argument
The class is called as
Person('Alex'). The string 'Alex' is the value supplied for the initialization data. - 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'. - 3Attribute assignment
The statement
self.name= name stores the value from the name parameter as the object's name instance attribute. - 4Returned 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.
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.
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.
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.
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__
Common mistake Leaving out self as the first parameter
What it looks likedef __init__(name):Why it failsThe source specifies that
__init__must include self as its first parameter. self represents the object being initialized.Do this insteadUse def __init__(self, name): and pass only the initialization value when creating the object.
Common mistake Using the wrong method name
What it looks likedef init(self, name):Why it failsPython recognizes the initializer through the distinctive name with double underscores on both sides.
Do this insteadWrite the method name exactly as __init__.
Common mistake Assuming the caller passes self
What it looks likePerson(object, 'Alex')Why it failsPython handles self automatically when it invokes
__init__.Do this insteadCreate the object with the initialization value, such as Person('Alex').
Common mistake Forgetting to bind a parameter to an instance attribute
What it looks likedef __init__(self, name): name = nameWhy it failsThe source identifies
self.name= name as the assignment that stores the value as part of the object's state.Do this insteadUse self.name = name when the value should become an attribute of that object.
Practice the Data Trace
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__.
Reveal 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. - 2The method name has double underscores on both sides:
__init__. - 3self is the first parameter and represents the particular object being initialized; Python supplies it automatically.
- 4Arguments passed during instantiation become
__init__parameter values, and assignments such asself.name= name store them as instance attributes. - 5Defining
__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.