Category: React • Beginner
Published on 14 Feb 2026
Explanation
What are Dunder Methods? Dunder = Double UNDERscore They look like: __init__ __str__ __add__ __len__ 1.These are special methods 2.Python calls automatically in certain situations. 3.Should n't call them directly 4.Python calls behind the screen
Code Example
Explanation
#white-Why Dunder Methods Matter They let your custom objects behave like built-in types.
Code Example
Example: a + b len(x) print(obj) obj == other All of these use dunder methods internally.
Explanation
__init__ – Object Creation Called when an object is created.
Code Example
class User:
def __init__(self, name):
self.name = name
u = User("hackforge")
Explanation
__str__ vs __repr__ __str__ → human-friendly __repr__ → developer-friendly
Code Example
class Car:
def __init__(self, brand):
self.brand = brand
def __str__(self):
return f"Car brand: {self.brand}"
def __repr__(self):
return f"Car('{self.brand}')"
car = Car("Tesla")
print(car)
Explanation
__len__ Controls behavior of len().
Code Example
class Playlist:
def __init__(self, songs):
self.songs = songs
def __len__(self):
return len(self.songs)
p = Playlist(["song1", "song2", "song3"])
print(len(p)) # 3
Explanation
#white-Dunder methods are hooks in Python 1.Makes code clean, readable, Pythonic 2.Used heavily in frameworks & libraries