A Dive into Magic: Exploring Python’s Special Methods (Dunder Methods)
Learn the Magic of Python

Python, frequently praised for its ease of use and adaptability, conceals a world of magic. Behind the scenes, a group of specialized methods collectively referred to as “dunder methods” (short for “double underscore”) controls object behavior in ways that give Python its true dynamic nature. In this article, we will explore their powers.
1. __str__ and __repr__
class Spell:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Spell: {self.name}"
def __repr__(self):
return f"Spell({self.name!r})"
fireball = Spell("Fireball")
print(fireball) # Output: Spell: FireballPython promotes the art of object self-expression with __str__ and __repr__. The __str__ function returns a human-readable representation, whereas the __repr__ function returns an unambiguous representation. These techniques influence how your objects interact with developers, facilitating debugging and exploration.
2.__init__
class Wizard:
def __init__(self, name, power):
self.name = name
self.power = power
gandalf = Wizard("Gandalf", "Wizardry")When an instance is created, __init__ initializes the object’s characteristics. You control the initial state of your object by defining __init__. This approach is your entryway to sculpting items to your specifications, imbuing them with a distinct spirit at the beginning.
3.__len__
class PotionChest:
def __init__(self, potions):
self.potions = potions
def __len__(self):
return len(self.potions)
potions_chest = PotionChest(["Healing", "Invisibility", "Strength"])
print(len(potions_chest)) # Output: 3When objects include collections, __len__ gives them the ability to determine their length. This approach uses built-in APIs like len() to provide your objects the ability to show their size. Your objects will fit neatly into Python’s ecosystem if you use __len__.
4.__add__
class WizardCollection:
def __init__(self, wizards):
self.wizards = wizards
def __add__(self, other_collection):
combined_wizards = self.wizards + other_collection.wizards
return WizardCollection(combined_wizards)
fellowship = WizardCollection(["Gandalf", "Radagast"])
harry_potter = WizardCollection(["Harry", "Hermione"])
all_wizards = fellowship + harry_potterBecause of __add__, objects may use the + operator in Python as well as numbers. By creating this function, you enable objects to interact intuitively, pushing Python’s natural behavior further.
5.__eq__
class MagicalCreature:
def __init__(self, name):
self.name = name
def __eq__(self, other):
if isinstance(other, MagicalCreature):
return self.name == other.name
return False
phoenix = MagicalCreature("Fawkes")
griffin = MagicalCreature("Fawkes")
print(phoenix == griffin) # Output: TrueThe power of equality testing is unlocked with __eq__. You can identify when your items are equal by specifying how they compare. This technique allows your objects to take part in logical processes, which enriches their interactions.
Where are some other awesome resources?
There are always new things to learn. If you want to learn more about awesome resources on Python please check out the below link.
That’s all for today. I believe these Dunder Methods will help you a lot in your development journey.
If you know of any other beautiful Dunder Methods please share them in the comments. Until we meet again. Cheers!
Want to Connect? If you want to, you can connect with me on Twitter.
In Plain English
Thank you for being a part of our community! Before you go:
- Be sure to clap and follow the writer! 👏
- You can find even more content at PlainEnglish.io 🚀
- Sign up for our free weekly newsletter. 🗞️
- Follow us on Twitter, LinkedIn, YouTube, and Discord.






