Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Python · 04 Jul 2026 · Hindi + English

What is self in Python? Explained with Class Examples

self is the reference to the current object inside a class. Understand why every method needs it, what happens without it, and self vs cls.

What is self?

self is the reference to the current object — the specific object on which a method is being called. It is how a method knows whose data to work with when many objects exist from the same class.
class Student:
    def __init__(self, name, marks):
        self.name  = name      # THIS object's name
        self.marks = marks     # THIS object's marks

    def show(self):
        print(self.name, "scored", self.marks)

s1 = Student("Aman", 92)
s2 = Student("Priya", 88)

s1.show()    # self = s1 here
s2.show()    # self = s2 here
Aman scored 92 Priya scored 88

Same show() method, different output — because self pointed to a different object each time.

What Python does behind the scenes

s1.show()
# Python internally converts this to:
Student.show(s1)      # s1 is passed as self automatically

This is the whole secret: the object before the dot is automatically passed as the first argument. That first parameter must be there to receive it, and by convention we name it self.

What happens if you forget self?

class Student:
    def show():              # self missing!
        print("Hello")

s = Student()
s.show()
# TypeError: Student.show() takes 0 positional arguments
# but 1 was given
"...but 1 was given" — that mysterious 1 argument is the object itself, which Python passed automatically and the method had no parameter to receive. This error message is a classic interview question.

self.name vs name — the crucial difference

class Student:
    def __init__(self, name):
        self.name = name    # object variable: lives with the object
        name = "temp"       # local variable: dies when __init__ ends

    def greet(self):
        print("Hello", self.name)   # works
        # print(name)               # NameError: name not defined

s = Student("Aman")
s.greet()
Hello Aman

self.name is stored inside the object and survives across methods. Plain name is a temporary local variable.

3 facts to say in interviews

  • self is not a keyword — you could write def show(this): and use this.name. It works, but the convention is always self.
  • self must be the first parameter of every instance method.
  • You never pass self yourself — Python passes the object automatically when you call obj.method().

self क्या है?

self current object का reference है — वह specific object जिस पर method call हो रहा है. इसी से method को पता चलता है कि जब same class से कई objects बने हों, तो किसके data पर काम करना है.
class Student:
    def __init__(self, name, marks):
        self.name  = name      # ISI object का name
        self.marks = marks     # ISI object के marks

    def show(self):
        print(self.name, "scored", self.marks)

s1 = Student("Aman", 92)
s2 = Student("Priya", 88)

s1.show()    # यहां self = s1
s2.show()    # यहां self = s2
Aman scored 92 Priya scored 88

Same show() method, अलग output — क्योंकि self हर बार अलग object को point कर रहा था.

Python पीछे क्या करता है

s1.show()
# Python internally इसे बदल देता है:
Student.show(s1)      # s1 automatically self बनकर pass हुआ

यही पूरा secret है: dot से पहले वाला object automatically पहले argument के रूप में pass होता है. उसे receive करने के लिए पहला parameter होना ज़रूरी है, और convention से उसका नाम self रखते हैं.

self भूल जाएं तो क्या होता है?

class Student:
    def show():              # self missing!
        print("Hello")

s = Student()
s.show()
# TypeError: Student.show() takes 0 positional arguments
# but 1 was given
"...but 1 was given" — वह mysterious 1 argument object खुद है, जिसे Python ने automatically pass किया और method के पास receive करने का parameter नहीं था. यह error message classic interview question है.

self.name vs name — असली अंतर

class Student:
    def __init__(self, name):
        self.name = name    # object variable: object के साथ रहता है
        name = "temp"       # local variable: __init__ खत्म होते ही खत्म

    def greet(self):
        print("Hello", self.name)   # चलता है
        # print(name)               # NameError: name not defined

s = Student("Aman")
s.greet()
Hello Aman

self.name object के अंदर store होता है और सभी methods में available रहता है. सादा name temporary local variable है.

Interviews में बोलने के 3 facts

  • self keyword नहीं है — आप def show(this): लिखकर this.name use कर सकते हैं. चलेगा, लेकिन convention हमेशा self है.
  • self हर instance method का पहला parameter होना चाहिए.
  • self आप खुद कभी pass नहीं करते — obj.method() call करने पर Python object को automatically pass करता है.

Frequently Asked Questions

What is self in Python in simple words?

self is the reference to the current object — when you call obj.method(), Python automatically passes obj as the first argument, received by the parameter named self.

Is self a keyword in Python?

No. self is only a naming convention; any valid name works as the first parameter, but the entire Python community uses self.

Why does "takes 0 positional arguments but 1 was given" occur?

Because a method was defined without self, yet Python still passed the object automatically — the method had no parameter to receive it.