Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 52

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 जैसा व्यवहार करने देते हैं।

💡 Dunder = "double underscore"। ये methods Python पर्दे के पीछे trigger करती है।

__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)
Book: Python 101

__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))
3

__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)
150

__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))
True

__eq__ तय करता है == का मतलब। Default रूप से दो objects तभी बराबर हैं जब वे बिल्कुल एक ही object हों; यहाँ हम value से तुलना करते हैं, तो एक जैसे coordinates वाले दो points बराबर हैं।

आम गलतियाँ

  • Dunder methods सीधे call करना। print(b) लिखिए, b.__str__() नहीं — Python आपके लिए call करती है।
  • __str__ string न लौटाना। इसे text return करना चाहिए, 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) क्या हैं?
Magic methods ख़ास methods हैं जिनके नाम double underscores से शुरू और ख़त्म होते हैं, जैसे __init__ या __str__। इन्हें आप सीधे call नहीं करते — Python इन्हें printing, adding या objects की तुलना जैसे actions के लिए अपने-आप call करती है।
इन्हें dunder methods क्यों कहते हैं?
"Dunder" "double underscore" का छोटा रूप है। चूँकि ये methods हर तरफ़ दो underscores के साथ लिखे जाते हैं (__str__), Python programmers इन्हें dunder methods कहते हैं।
__str__ क्या करता है?
__str__ तय करता है print(object) क्या दिखाए। इसके बिना, object print करना memory address जैसा बेकार कुछ दिखाता है। इसके साथ, आप एक साफ़, पढ़ने योग्य text रूप control करते हैं।
अपने objects पर + कैसे काम कराऊँ?
अपनी class में __add__(self, other) define कीजिए। फिर Python इसे तब call करती है जब आप obj1 + obj2 लिखें। इसे operator overloading कहते हैं — operators को अपनी class के लिए अर्थ देना।
__eq__ क्या control करता है?
__eq__(self, other) तय करता है आपके objects के लिए == का क्या मतलब है। Default रूप से, == जाँचता है वे एक ही object हैं या नहीं; __eq__ से आप उन्हें उनकी values से तुलना कर सकते हैं।
← Back to Python Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicself and __init__ आपकी प्रगति: 52 / 66 (79%) · Lesson 53 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें