Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 53

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.

💡 __init__ = runs when the object is created. self = "this particular object".

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)
Aman 12

__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())
Hi, Aman

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, Aman
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)
Aman 0

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 self in a method. Every method needs it as the first parameter.
  • Passing self in the call. Write obj.greet(), not obj.greet(obj) — Python adds it.
  • Using name instead of self.name. Without self, 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.
  • self means "this object" and is every method's first parameter.
  • Store data with self.attribute so each object keeps its own.
  • Python passes self automatically 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?
When you call 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?
No. Python passes it automatically. You write 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.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicInheritance & Polymorphism Your progress: 53 / 66 (80%) · Lesson 54 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free