The __init__ Method
The __init__ method is a special method that runs automatically when you create an object, allowing you to initialize its state immediately.
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…
When Objects Come to Life
When you create an object, Python automatically runs a special method named __init__. This gives the object its initial state immediately, before your code receives the object back. Instead of creating an object first and then setting its attributes through separate manual steps, you define the setup once inside __init__, so every instance follows the same initialization process.
What do you think happens?
Suppose a class has an __init__ method and you create an object with MyClass(some_value). When does __init__ run?
Reveal answer
Automatically during object creation, before your code receives the object
Every instantiation automatically triggers __init__. The method runs on the newly created object before the surrounding code gets that object back.
The Creation Sequence
The important order is instantiation, automatic execution of __init__, and then return of the initialized object to your code. The call MyClass(some_value) therefore does more than identify a class. It also starts the initialization process for the new object.
Naming and Parameters
__init__ is a special method whose name has two underscores before init and two underscores after it. Its first parameter must be self. Additional parameters can receive the values supplied when an object is instantiated.
In this generated example, __init__ has the required self parameter followed by username, an initialization parameter. The assignment uses self to bind the received username to an instance attribute named username. That binding makes the value part of the object's state.
From Arguments to State
Initializing a Profile
Trace the value in the generated expression Profile("Ari") through the __init__ method.
- 1Instantiation argument
The string "Ari" is supplied when Profile is instantiated.
- 2__init__ parameter
The supplied value is received by the parameter named username.
- 3Attribute binding
Inside
__init__,self.username= username uses self to bind the parameter value to the object's username attribute. - 4Initialized state
The resulting object has username set to "Ari" as part of its initial state.
The value travels from the instantiation argument to username and then to self.username.
AriThe assignment inside __init__ is the step that transfers the parameter value into object state. self refers to the particular instance being initialized, while username on the right side represents the value received by the parameter. The username on the left side identifies the attribute stored on that instance.
State Before and After
The diagram shows the role of __init__ as an initialization step: it sets up the object's initial state. After the method binds a value through self, that value becomes an instance attribute and is available as part of the object. Because the method runs automatically for every instantiation, each instance receives the initialization process without a separate manual setup step.
Mistakes with Initialization
Common mistake Using the wrong method name
What it looks likedef init(self, username):Why it failsPython recognizes the special automatic behavior through the exact name
__init__, including both pairs of underscores.Do this insteadUse def __init__(self, username):Common mistake Leaving out self as the first parameter
What it looks likedef __init__(username):Why it failsThe
__init__method must include self as its first parameter.Do this insteadPlace self first, followed by any initialization parameters you need.
Common mistake Failing to use self when binding a parameter to object state
What it looks likeusername = usernameWhy it failsThe source method for making a parameter part of the object's state is to use self to bind it to an instance attribute.
Do this insteadBind the value as self.username = username.
Common mistake Expecting to initialize each object manually after creation
What it looks likeCreate an object first, then treat initialization as a separate required setup process.
Why it failsEvery instantiation automatically triggers
__init__, so initialization is defined in the class and runs as part of object creation.Do this insteadPut the initial-state setup inside __init__.
Define the object's initial-state setup in __init__ rather than repeating separate setup steps for each instance. This keeps initialization consistent and makes the path from instantiation arguments to instance attributes explicit.
Trace It Yourself
Consider the generated class declaration: class Account: def __init__(self, owner): self.owner = owner. For the instantiation Account("Mina"), identify the instantiation argument, the __init__ parameter, and the resulting instance attribute value.
Hints
- Find the value supplied inside
Account(...). - Match that value to the parameter after self.
- Follow the assignment that begins with self.
Practice Answer
Trace Account("Mina") through __init__.
- 1Argument
"Mina" is supplied during instantiation.
- 2Parameter
The value is received by owner.
- 3Attribute
self.owneris assigned the value received by owner.
The object's owner attribute is initialized with "Mina".
Key Takeaways
- 1
__init__is a special method that runs automatically when an object is created. - 2The method name uses double underscores on both sides, and self must be its first parameter.
- 3Values supplied during instantiation can be received by
__init__parameters. - 4Using self to bind those parameters creates instance attributes and establishes the object's initial state.
- 5Every instantiation triggers the initialization process, promoting consistent object setup.
At a glance
__init__runs automatically during object creation, before the object is returned to your code.- Its exact name includes double underscores at the beginning and end.
- self must be the first parameter, followed by parameters for values needed during initialization.
- Assignments such as
self.attribute= parameter use self to place values into instance state. - The data path is instantiation argument,
__init__parameter, and then instance attribute.