Dunder_Methods.md
November 28, 2023 · View on GitHub
Object-Oriented Programming: Dunder Methods
Dunder methods, also known as magic methods or special methods, are a set of predefined methods in Python that begin and end with double underscores (__). These methods allow you to emulate the behavior of built-in types or to implement operator overloading.
Key Dunder Methods
-
__init__(self, [...]):- The constructor method of a class, called when an object is created.
- Example:
class MyClass: def __init__(self, value): self.value = value
-
__str__(self):- Returns the informal string representation of an object, readable by humans.
- Example:
class MyClass: # ... def __str__(self): return str(self.value)
-
__repr__(self):- Returns the official string representation of an object, which should be unambiguous.
- Example:
class MyClass: # ... def __repr__(self): return f"MyClass({self.value})"
-
Arithmetic Dunders:
__add__,__sub__,__mul__, etc., for arithmetic operations.- Example:
class MyClass: # ... def __add__(self, other): return MyClass(self.value + other.value)
- Example:
-
Comparison Dunders:
__eq__,__lt__,__gt__, etc., for comparison operations.- Example:
class MyClass: # ... def __eq__(self, other): return self.value == other.value
- Example:
-
__len__(self):- Returns the length of the container.
- Example:
class MyCollection: def __init__(self): self.items = [] def __len__(self): return len(self.items)
-
__getitem__(self, key)and__setitem__(self, key, value):- For indexing and slicing.
- Example:
class MyCollection: # ... def __getitem__(self, key): return self.items[key] def __setitem__(self, key, value): self.items[key] = value
Importance of Dunder Methods
- Emulating Built-in Behaviors: They allow your objects to behave like built-in types. For example, adding two objects with
+or getting the length of an object withlen(). - Pythonic Practices: Using dunder methods is considered Pythonic and can make your code more intuitive and elegant.
- Operator Overloading: They provide an elegant way to implement operator overloading.
Example of Dunder Methods in Use
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Vector({self.x}, {self.y})"
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# Usage
v1 = Vector(2, 4)
v2 = Vector(1, -1)
print(v1 + v2) # Output: Vector(3, 3)
In this example, Vector implements __add__ to allow adding two Vector instances using the + operator and __repr__ to provide a meaningful string representation.
Conclusion
Dunder methods are a fundamental part of Python’s object model. They allow developers to integrate their custom objects more seamlessly with Python's language features, resulting in more natural and readable code. Understanding and using dunder methods is an essential part of writing idiomatic Python code.