Concept Flow - __init__ method behavior
Create object instance
Call __init__ method
Initialize attributes
Return new object
Object ready to use
When you create a new object, Python calls __init__ to set up initial values inside the object.
class Dog: def __init__(self, name): self.name = name d = Dog('Buddy') print(d.name)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Create Dog object with 'Buddy' | Calls Dog.__init__(self, 'Buddy') | New Dog object created |
| 2 | Inside __init__, assign self.name = 'Buddy' | self.name set to 'Buddy' | Object attribute name initialized |
| 3 | Return from __init__ | No return value (returns None) | Object fully initialized |
| 4 | Print d.name | Access attribute d.name | Output: Buddy |
| Variable | Start | After Step 2 | Final |
|---|---|---|---|
| self.name | undefined | 'Buddy' | 'Buddy' |
| d | undefined | Dog object created | Dog object with name='Buddy' |
__init__ is a special method called when creating an object. It sets up initial attributes using self. self.name = value stores data inside the object. __init__ does not return the object; Python does that. Use __init__ to prepare your object for use.