Magic (Dunder) Methods in Python
In this lesson: Customise Python classes with dunder methods — __init__, __str__, __repr__, __len__, __add__ — with output. In Hindi & English.
What are magic methods
Magic methods (also called dunder methods, for "double underscore") are special methods with names like __str__ and __add__. You never call them directly — Python calls them automatically when you print an object, add two objects, check their length, and more. They let your class behave like a 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__ decides what print(b) shows. Without it, printing an object gives an unhelpful memory address; with it, you get clean, readable 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__ is called when you use len(t) on your object. Here it returns how many members the team has, so len() works naturally.
__add__ — the + 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__ defines what + means for your class — this is called operator overloading. Now Money(100) + Money(50) makes sense and returns a new Money of 150.
__eq__ — comparison
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__ defines what == means. By default two objects are equal only if they are the exact same object; here we compare by value, so two points with the same coordinates are equal.
Common mistakes
- Calling dunder methods directly. Write
print(b), notb.__str__()— Python calls them for you. __str__not returning a string. It mustreturntext, not print it.- Wrong spelling. Two underscores each side:
__add__, not_add_. - Forgetting
other. Methods like__add__and__eq__take a second object to work with.
Summary
- Magic (dunder) methods have double underscores and are called automatically.
__str__controls whatprint()shows.__len__makeslen()work on your object.__add__defines the+operator (operator overloading).__eq__defines how==compares your objects.
Next, revise classes with self and __init__, or extend them with inheritance and polymorphism.
Frequently Asked Questions
What are magic methods (dunder methods) in Python?
__init__ or __str__. You do not call them directly — Python calls them automatically for actions like printing, adding or comparing objects.Why are they called dunder methods?
__str__), Python programmers call them dunder methods.What does __str__ do?
__str__ decides what print(object) shows. Without it, printing an object shows something unhelpful like a memory address. With it, you control a clean, readable text form.How do I make + work on my own objects?
__add__(self, other) in your class. Then Python calls it when you write obj1 + obj2. This is called operator overloading — giving operators meaning for your class.What does __eq__ control?
__eq__(self, other) defines what == means for your objects. By default, == checks if they are the same object; with __eq__ you can compare by their values instead.