Python में self क्या है? Class Examples के साथ समझें
self class के अंदर current object का reference है. समझें हर method को यह क्यों चाहिए, इसके बिना क्या होता है, और 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
Python में self आसान शब्दों में क्या है?
self current object का reference है — जब आप obj.method() call करते हैं, Python obj को automatically पहले argument के रूप में pass करता है, जिसे self नाम का parameter receive करता है.
क्या self Python का keyword है?
नहीं. self सिर्फ naming convention है; पहले parameter के रूप में कोई भी valid नाम चलेगा, लेकिन पूरी Python community self use करती है.
"takes 0 positional arguments but 1 was given" error क्यों आता है?
क्योंकि method बिना self के define हुआ, फिर भी Python ने object automatically pass किया — method के पास उसे receive करने का parameter नहीं था.