Class Definition and Structure
The __init__ method is a special method that runs automatically when you create an object, allowing you to initialize its state immediately.
Objects Before Initialization
Creating an object is more than producing an empty instance. When you create an object from a class, Python automatically runs the class's __init__ method. That method sets up the object's initial state before the object is returned to your code. The result is an object whose required attributes are already available instead of one that needs a series of manual setup steps.
The Initializer's Role
The __init__ method is a special method that runs automatically when you create an object. Its purpose is to initialize the object's state immediately. You define the setup process once inside the class, and every object created from that class goes through that process automatically.
__init__ is a special method recognized by Python and called automatically during object creation so that the new object can receive its initial state.
Arguments Become Attributes
The data flow has three stages. First, an argument is supplied when the class is instantiated. Next, Python passes that value into the matching parameter of __init__. Finally, an assignment such as self.name = name binds the parameter's value to an instance attribute. The value then becomes part of that particular object's state.
Tracing Person('Alex')
Determine how the value 'Alex' reaches the resulting Person object's state.
Instantiation: The class is instantiated with 'Alex' as its argument.
Automatic invocation: Python creates a Person object and immediately calls its __init__ method.
Parameter binding: The name parameter inside __init__ receives the value 'Alex'. The self parameter represents the object being initialized.
Attribute assignment: The assignment self.name = name stores 'Alex' as the object's name attribute.
Return to code: After __init__ finishes, the object is returned to the code that created it, with its name attribute already set.
The resulting Person object has a name attribute containing 'Alex'.
Method Naming and Parameters
The initializer has a distinctive name: two underscores before init and two underscores after it. This naming convention identifies it as a special method with automatic behavior. Like any method, it receives self as its first parameter. After self, you list the parameters needed to initialize the object. When the class is instantiated, you provide values for those initialization parameters, not for self; Python supplies self automatically.
| Part | Role |
|---|---|
| __init__ | The special method name recognized for automatic initialization |
| self | The object being initialized |
| name | An initialization parameter that receives data supplied during instantiation |
| self.name | The instance attribute where the value is stored |
Parts of the initializer structure
State Changes During Setup
Before initialization, the newly created Person object exists but has no initialized attributes. During __init__, the supplied name value is assigned through self.name. After the method completes, the same object has a name attribute containing 'Alex'. This is the central state change performed by the initializer.
A class can require several pieces of initialization data. In the source example, instantiating Student with 'Jordan', 12345, and 10 causes __init__ to run with those three arguments. The resulting object has its attributes populated immediately, so no separate attribute-setting steps are needed afterward.
Design Benefits
Define the initialization requirements in __init__ rather than relying on later manual assignments. This makes the setup process automatic and consistent for every instance. It also communicates what data an object needs: a declaration such as __init__(self, name, age) tells readers that a Person requires a name and an age.
Common Initialization Mistakes
Writing the initializer without double underscores on both sides of init
The distinctive double-underscore name is what identifies this method as the special method Python calls automatically.
Fix:
Use __init__ with two underscores before init and two underscores after it.Omitting self as the first parameter
self represents the object being initialized and is required as the first parameter of the method.
Fix:
Place self first, followed by the parameters needed for initialization.Trying to provide a value for self during instantiation
Python supplies self automatically when it invokes __init__.
Fix:
Pass values only for the initialization parameters that follow self.Leaving the parameter as a local setup value instead of binding it to the object
The value is not made part of the object's instance state unless it is assigned to an instance attribute.
Fix:
Use self to bind the parameter to an instance attribute, as in self.name = name.Assuming the object is returned before initialization finishes
The source describes __init__ as running immediately, before the object is returned to your code.
Fix:
Think of object creation as completing the automatic initialization step before your code receives the object.
Check Your Trace
Trace the data flow for an object created as Student('Jordan', 12345, 10). Identify the three instantiation arguments, the corresponding __init__ parameters, and the instance attributes that should be populated when initialization finishes.
Hints
- Python passes the three supplied values into __init__ automatically.
- self represents the Student object being initialized.
- Each initialization value becomes part of the object's state when it is bound to an instance attribute.
What do you think happens?
When Person('Alex') finishes, does the resulting object already have its name attribute?
Reveal answer
Answer: Yes, because __init__ has already assigned it
Python calls __init__ automatically during object creation. The assignment self.name = name stores 'Alex' before the object is returned to your code.
Key Takeaways
- __init__ is a special method that Python calls automatically when an object is created.
- The method name uses two underscores before and after init, and self must be its first parameter.
- Values supplied during instantiation become __init__ parameter values, while self is supplied automatically.
- Assignments such as self.name = name store initialization data as instance attributes.
- The object is returned to your code only after __init__ has established its initial state.