
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Python में कुछ ऐसे pre-defined methods होते है जो specific situations में automatically call हो जाते है, इन methods को Dunder Method कहा जाता है। dunder methods के आगे और पीछे double underscore “__” लगा होता है। इन methods को “Magic Method” के नाम से भी जाना जाता है।
Dunder methods का use हम classes and Objects मे करते है और कहीं इसका use आपको जल्दी नहीं मिलेगा।
class MyList:
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
def __str__(self):
return f"MyList({self.data})"
def __add__(self, other):
return MyList(self.data + other.data)
ml1 = MyList([1,2,3])
ml2 = MyList([4,5])
print(len(ml1)) # __len__ → 3
print(ml1[0]) # __getitem__ → 1
print(ml1) # __str__ → MyList([1,2,3])
ml3 = ml1 + ml2 # __add__
print(ml3) # MyList([1,2,3,4,5])
Dunder Function | Purpose | Small Example |
__init__(self, …) | Constructor – object banate समय call होता है | obj = MyClass() |
__del__(self) | Destructor – object delete होते समय call | del obj |
__str__(self) | Human readable string (print) | print(obj) |
__repr__(self) | Official string (debugging/logging) | repr(obj) |
__len__(self) | Object की length बताता है | len(obj) |
__getitem__(self, key) | Indexing support देता है | obj[0] |
__setitem__(self, key, value) | Index पर value assign करता है | obj[1] = 10 |
__delitem__(self, key) | Index वाली value delete करता है | del obj[2] |
__iter__(self) | Iterable बनाता है | for x in obj: |
__next__(self) | Next value return करता है | next(obj) |
__call__(self, …) | Object को function की तरह call कर सकते हैं | obj() |
__contains__(self, item) | Membership test | if 5 in obj: |
__eq__(self, other) | Equal (==) operator | obj1 == obj2 |
__lt__(self, other) | Less than (<) operator | obj1 < obj2 |
__gt__(self, other) | Greater than (>) operator | obj1 > obj2 |
__add__(self, other) | Addition (+) operator | obj1 + obj2 |
__sub__(self, other) | Subtraction (-) operator | obj1 – obj2 |
__mul__(self, other) | Multiplication (*) | obj1 * obj2 |
__truediv__(self, other) | Division (/) | obj1 / obj2 |
__floordiv__(self, other) | Floor Division (//) | obj1 // obj2 |
__mod__(self, other) | Modulo (%) | obj1 % obj2 |
__pow__(self, other) | Power (**) | obj1 ** obj2 |
__enter__(self) | Context manager start | with obj: |
__exit__(self, exc_type, exc_val, exc_tb) | Context manager end | with obj: |
उम्मीद करते है कि आपको Dunder Methods अच्छे समझ मे आ गया होगा। अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...