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?
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
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
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()
self.name is stored inside the object and survives across methods. Plain name is a temporary local variable.
3 facts to say in interviews
selfis not a keyword — you could writedef show(this):and usethis.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 क्या है?
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
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
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()
self.name object के अंदर store होता है और सभी methods में available रहता है. सादा name temporary local variable है.
Interviews में बोलने के 3 facts
selfkeyword नहीं है — आपdef show(this):लिखकरthis.nameuse कर सकते हैं. चलेगा, लेकिन 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.