self and __init__ in Python
In this lesson: What self means and how __init__ constructor works in Python classes — simple examples with output. In Hindi & English.
What are self and __init__
When you build a class in Python, two things appear again and again: __init__ and self. __init__ is the constructor — it sets up a new object. self is how a method refers to the object it belongs to. Together they let each object hold its own data.
The __init__ constructor
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
s = Student("Aman", 12)
print(s.name, s.roll)
__init__ runs automatically when you write Student("Aman", 12). It receives the values and stores them on the object with self.name and self.roll.
Why every method needs self
class Student:
def __init__(self, name):
self.name = name
def greet(self):
return "Hi, " + self.name
s = Student("Aman")
print(s.greet())
Every method takes self first, so it knows which object it is working on. self.name reads that object's own name. When you call s.greet(), Python passes s as self automatically.
Each object has its own data
a = Student("Aman")
b = Student("Diya")
print(a.greet()) # Hi, Aman
print(b.greet()) # Hi, Diya
Hi, Diya
Because data is stored on self, each object keeps its own copy. a and b are separate students with separate names — that is the whole point of self.
Default values in __init__
class Student:
def __init__(self, name, marks=0):
self.name = name
self.marks = marks
s = Student("Aman") # marks defaults to 0
print(s.name, s.marks)
You can give a parameter a default in __init__, so it is optional. Here marks defaults to 0 if the caller does not provide it.
Common mistakes
- Forgetting
selfin a method. Every method needs it as the first parameter. - Passing self in the call. Write
obj.greet(), notobj.greet(obj)— Python adds it. - Using
nameinstead ofself.name. Withoutself, it is a temporary local variable. - Wrong dunder spelling. It is
__init__with two underscores each side.
Summary
__init__is the constructor — it runs when an object is created.selfmeans "this object" and is every method's first parameter.- Store data with
self.attributeso each object keeps its own. - Python passes
selfautomatically when you call a method. - You can give
__init__parameters default values.
Next, extend classes with inheritance and polymorphism, or explore magic methods.
Frequently Asked Questions
What is __init__ in Python?
__init__ is the constructor — a special method that runs automatically when you create an object. It sets up the object's starting values. For example, giving a new Student their name and roll number the moment they are created.What does self mean in Python?
self refers to the current object — the specific one a method is working on. You use it to store and read that object's own data, like self.name. It is always the first parameter of a method.Why must self be the first parameter?
obj.method(), Python automatically passes the object as the first argument. So the method must have self there to receive it. Forgetting it causes a "missing argument" error.Do I pass self myself when calling a method?
obj.greet(), not obj.greet(obj). You only include self in the definition, never in the call.What is the difference between self.name and name?
self.name is an attribute stored on the object and lives as long as the object does. A plain name inside a method is a local variable that disappears when the method ends.