Magic Methods (Dunder)
Magic methods क्या हैं
Magic methods (जिन्हें dunder methods भी कहते हैं, "double underscore" के लिए) ख़ास methods हैं जिनके नाम __str__ और __add__ जैसे होते हैं। इन्हें आप कभी सीधे call नहीं करते — Python इन्हें तब अपने-आप call करती है जब आप object print करें, दो objects जोड़ें, उनकी length जाँचें, वग़ैरह। ये आपकी class को built-in type जैसा व्यवहार करने देते हैं।
__str__ — printable text
class Book:
def __init__(self, title):
self.title = title
def __str__(self):
return "Book: " + self.title
b = Book("Python 101")
print(b)
__str__ तय करता है print(b) क्या दिखाए। इसके बिना, object print करना बेकार memory address देता है; इसके साथ, आपको साफ़, पढ़ने योग्य output मिलता है।
__len__ — length
class Team:
def __init__(self, members):
self.members = members
def __len__(self):
return len(self.members)
t = Team(["Aman", "Diya", "Kabir"])
print(len(t))
__len__ तब call होता है जब आप अपने object पर len(t) इस्तेमाल करें। यहाँ यह लौटाता है team में कितने members हैं, तो len() स्वाभाविक रूप से चलता है।
__add__ — + operator
class Money:
def __init__(self, amount):
self.amount = amount
def __add__(self, other):
return Money(self.amount + other.amount)
total = Money(100) + Money(50)
print(total.amount)
__add__ तय करता है आपकी class के लिए + का मतलब — इसे operator overloading कहते हैं। अब Money(100) + Money(50) का अर्थ बनता है और 150 का नया Money लौटाता है।
__eq__ — तुलना
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
print(Point(1, 2) == Point(1, 2))
__eq__ तय करता है == का मतलब। Default रूप से दो objects तभी बराबर हैं जब वे बिल्कुल एक ही object हों; यहाँ हम value से तुलना करते हैं, तो एक जैसे coordinates वाले दो points बराबर हैं।
आम गलतियाँ
- Dunder methods सीधे call करना।
print(b)लिखिए,b.__str__()नहीं — Python आपके लिए call करती है। __str__string न लौटाना। इसे textreturnकरना चाहिए, print नहीं।- गलत spelling। हर तरफ़ दो underscores:
__add__,_add_नहीं। otherभूलना।__add__और__eq__जैसे methods काम करने को दूसरा object लेते हैं।
सारांश
- Magic (dunder) methods में double underscores होते हैं और अपने-आप call होते हैं।
__str__control करता हैprint()क्या दिखाए।__len__आपके object परlen()चलाता है।__add__+operator तय करता है (operator overloading)।__eq__तय करता है==आपके objects की तुलना कैसे करे।
आगे, classes दोहराइए self और __init__ से, या उन्हें बढ़ाइए inheritance और polymorphism से।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Python में magic methods (dunder methods) क्या हैं?
__init__ या __str__। इन्हें आप सीधे call नहीं करते — Python इन्हें printing, adding या objects की तुलना जैसे actions के लिए अपने-आप call करती है।इन्हें dunder methods क्यों कहते हैं?
__str__), Python programmers इन्हें dunder methods कहते हैं।__str__ क्या करता है?
__str__ तय करता है print(object) क्या दिखाए। इसके बिना, object print करना memory address जैसा बेकार कुछ दिखाता है। इसके साथ, आप एक साफ़, पढ़ने योग्य text रूप control करते हैं।अपने objects पर + कैसे काम कराऊँ?
__add__(self, other) define कीजिए। फिर Python इसे तब call करती है जब आप obj1 + obj2 लिखें। इसे operator overloading कहते हैं — operators को अपनी class के लिए अर्थ देना।__eq__ क्या control करता है?
__eq__(self, other) तय करता है आपके objects के लिए == का क्या मतलब है। Default रूप से, == जाँचता है वे एक ही object हैं या नहीं; __eq__ से आप उन्हें उनकी values से तुलना कर सकते हैं।