Back to all posts

Magic (Dunder) Methods in Python

Magic methods (also known as dunder methods , short for double underscore ) in Python are special methods that have double underscores before and after the…

Magic methods (also known as dunder methods, short for double underscore) in Python are special methods that have double underscores before and after their names, like __init__, __str__, __add__, etc.

They are not magical in the supernatural sense but give your objects the power to interact with built-in Python features like operators, functions, and even loops!


✨ Why use magic methods?

Magic methods allow your custom objects to behave like built-in types (int, str, list, etc.). This means you can:

  • Initialize objects (__init__)
  • Use operators like +, -, < (__add__, __lt__)
  • Represent objects as strings (__str__, __repr__)
  • Get length using len() (__len__)
  • Access items like dictionaries/lists (__getitem__, __setitem__)
  • Use loops (__iter__, __next__)
  • Compare objects (__eq__, __lt__, etc.)

🔧 Common Magic Methods and Their Uses

Magic MethodPurposeExample
__init__ConstructorPerson("Himanshu")
__str__User-friendly stringprint(obj)
__repr__Official string (for debugging)repr(obj)
__len__Length of objectlen(obj)
__getitem__Access elementsobj
__setitem__Set elementsobj = value
__delitem__Delete elementsdel obj
__iter__Make object iterablefor x in obj:
__next__Next element in iterationnext(obj)
__add__Add two objectsobj1 + obj2
__sub__Subtract objectsobj1 - obj2
__eq__Equality checkobj1 == obj2
__lt__Less than checkobj1 < obj2
__call__Make object callable like a functionobj()
__contains__Membership test (in)"x" in obj
Python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return f"Point({self.x}, {self.y})"

    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

# Example usage
p1 = Point(1, 2)
p2 = Point(3, 4)

print(p1)  # Output: Point(1, 2)
print(p2)  # Output: Point(3, 4)

p3 = p1 + p2
print(p3)  # Output: Point(4, 6)

print(p1 == p2)  # Output: False
print(p1 == Point(1, 2))  # Output: True
Python
class Person:
    def __init__(self, name, age):
            self.name = name
            self.age = age

    def __str__(self):
        return f"Person({self.name}, {self.age})" # User-friendly

    def __repr__(self):
         return f"Person(name='{self.name}', age={self.age})" # Unambiguous, for debugging
    

p = Person("Alice", 30)
print(str(p)) # Person(Alice, 30)
print(repr(p)) # Person(name='Alice', age=30)
print(p) # Person(Alice, 30)

🧠 Tips

  • Not all magic methods are required — use only those that make sense for your object.
  • __str__ is for humans, __repr__ is for developers.

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.