What is the __init__ Method in Python?
Learn what Python's __init__ method does: the constructor that initialises object attributes automatically, with examples, defaults, and inheritance tips.
Expected Interview Answer
__init__ is Python's constructor: a special method that runs automatically right after a new object is created, used to initialise the object's attributes with starting values.
When you call ClassName(args), Python creates the instance and then calls __init__(self, args) on it, where self is the new object. You assign self.attribute = value inside __init__ to give each instance its own state. It is one of Python's 'dunder' (double-underscore) methods; it does not create the object itself (that is __new__) but configures it. __init__ can define default parameter values and can call super().__init__() to run a parent class's initialiser.
- Sets each object's starting state automatically
- Guarantees required attributes exist from creation
- Supports default and required parameters
- Centralises setup logic in one place
- Integrates with inheritance via super().__init__()
AI Mentor Explanation
Before a match, each new player is kitted out: given a jersey number, a bat and a role. __init__ is that kitting-out moment — it runs the instant a player object is created and stamps in their starting details so they walk onto the field ready.
Step-by-Step Explanation
Step 1
Define __init__
Add def __init__(self, ...): to your class, with self first followed by any parameters the object needs at creation.
Step 2
Assign attributes
Inside the method, bind incoming values to the instance with self.name = name so each object stores its own state.
Step 3
Let Python call it
When you write obj = MyClass(args), Python creates the object and automatically invokes __init__(obj, args) — you never call it directly.
Step 4
Provide defaults
Give parameters default values, e.g. def __init__(self, balance=0):, so callers can omit optional arguments.
Step 5
Chain to the parent
In a subclass, call super().__init__(...) inside __init__ to run the parent's initialiser before adding child-specific attributes.
What Interviewer Expects
- __init__ is the constructor/initialiser
- It runs automatically after object creation
- self.attribute assignments set instance state
- Difference between __init__ and __new__
- Use of super().__init__() with inheritance
Common Mistakes
- Calling __init__ a true constructor when __new__ actually creates the object
- Forgetting self as the first parameter
- Returning a value from __init__ (it must return None)
- Using a mutable default argument like a list, which is shared across instances
- Not calling super().__init__() in a subclass and skipping parent setup
Best Answer (HR Friendly)
“__init__ is a special setup method that runs automatically whenever you create a new object from a class. Its job is to fill in the object's starting details, so every new object begins life with the information it needs.”
Code Example
class Car:
def __init__(self, make, model, year=2024):
self.make = make # set on creation
self.model = model
self.year = year
self.mileage = 0
def describe(self):
return f"{self.year} {self.make} {self.model}"
c = Car("Toyota", "Corolla") # __init__ runs now
print(c.describe()) # 2024 Toyota Corolla
print(c.mileage) # 0
# __init__ returns None, never a valueFollow-up Questions
- What is the difference between __init__ and __new__?
- Why should you avoid mutable default arguments in __init__?
- Can a class work without an explicit __init__ method?
- How does __init__ interact with inheritance and super()?
- What happens if __init__ returns a value?
MCQ Practice
1. When does __init__ run?
Python calls __init__ automatically right after creating a new instance, to initialise its attributes.
2. What must __init__ return?
__init__ initialises the already-created object and must return None; returning anything else raises a TypeError.
3. Which method actually creates the object before __init__ configures it?
__new__ allocates and returns the new instance; __init__ then initialises that instance's attributes.
Flash Cards
What is __init__? — A special method that runs automatically after object creation to initialise the instance's attributes.
Is __init__ the constructor? — It is the initialiser. __new__ creates the object; __init__ sets up its state afterward.
What must __init__ return? — None. Returning any other value raises a TypeError.
Why avoid mutable defaults? — A default like [] is created once and shared across all instances, causing surprising shared state; use None and create inside instead.