“Python’s Plumbing” Is Not As Flashy as “Magic Methods” • But Is It Better?
Dunder methods are the pipes and valves that are out of sight, but that keep everything flowing in Python
You’ve heard this phrase before: “Everything is an object in Python”. But here’s another phrase that’s related but rather less catchy:
Everything goes through special methods (dunder methods) in Python
Special methods are everywhere. However, you don’t see them. In fact, you’re not meant to use them directly unless you’re defining them within a class. They’re out of sight. But they keep everything moving smoothly in Python.
Here’s a short essay exploring where these special methods fit within Python.
Why Should I Care About Special Methods?
“Everything goes through special methods in Python.” I should probably preface the phrase with “almost”, but the phrase is already unwieldy as it is.
You want to display an object on the screen? Python looks for guidance in .__str__() or .__repr__().
You want to use the object in a for loop? Is it even possible? Python looks for .__iter__() to check whether it can iterate over the object and how.
Do you want to fetch an individual item from the object? If this makes sense for this object, then it will have a .__getitem__() special method.
How should Python interpret an object’s truthiness? There’s .__bool__() for that. Or .__len__()!
Ah, you’d like to add an object to another object. Does your object have a .__add__() special method?
I could go on. I’ll post links to articles that cover some of these special methods in more detail below.
I’m also running a two-hour workshop this Thursday, 26 February, called Python’s Plumbing • Dunder Methods and Python’s Hidden Interface
Many operations you take for granted in your code are governed by special methods. Each class defines the special methods it needs, and Python knows how to handle instances of that class through those methods.
But let’s talk about different ways we refer to these special methods.
What’s In A Name?
These methods are called special methods. That’s their official name. These special methods have double underscores at the beginning and end of their names, such as .__init__(), .__str__(), and .__iter__(). This double-underscore notation led to the informal name “dunder methods”. And let’s face it, most people call them “dunder methods” instead of their actual name: “special methods.”
I often call them dunder methods, too. However, the term dunder merely describes the syntax, double underscore, so it doesn’t tell us much about what they do. The term special doesn’t tell us what they do, either, but it shows us they have a special role in Python.
There’s No Such Thing as Magic
Some people also call them magic methods. However, I avoid this term, and I discourage students from using it. It makes these methods look unnecessarily mysterious, perhaps difficult to understand because it’s all down to magic.
But there’s no such thing as magic (unless you’re Harry Potter). And the magic tricks we see from real-world “magicians” are just that – tricks. The magic dissolves away once you know how the trick works.
And if you’re learning Python, then you need to learn how to be a magician. You need to learn the “magic tricks.” Therefore, they’re no longer magic!
Python’s Plumbing (”Plumbing Methods”, Anyone?)
So, “special method” tells us that these methods are important, but it doesn’t tell us what they do. “Dunder method” describes the syntax. “Magic method” misleads us and doesn’t provide any useful insight.
How about “plumbing methods” then? Now, before anyone takes me too seriously, I’m saying this with my tongue firmly in my cheek. I’m not that foolish to suggest a new term for the whole Python community to adopt. And it’s not as flashy as “magic methods” or as cool as “dunder methods”. But bear with me…
Let’s explore the analogy, even if the term won’t catch on.
Disclaimer: I know very little about plumbing. But I think that’s OK for this essay!
There are pipes, valves, and other stuff carrying water (clean or otherwise) around your house. You know they’re there. You need them there. But you don’t see them.
You don’t think about these pipes unless you’re building the house – or unless the pipes are blocked or leaking.
The house’s plumbing keeps things running smoothly. Yet, it’s out of sight, and you don’t normally think about it. You take it for granted.
You see where I’m going, right?
Python’s special methods perform the same role. You don’t normally see them when coding since they’re called implicitly, behind the scenes. You do need to define the special methods you need when you’re defining the class, just like you’ll need to lay the pipes when building or modifying your house.
And if something goes wrong in your code, you may need to dive into how these dunder methods behave, just as when you have a leak and need to explore which pipe is responsible.
Good plumbing is reliable, predictable. Bad plumbing is asking for trouble. The same applies to the infrastructure you create through the special methods you define in classes.
So, there you go, they’re “plumbing methods”. This name tells us what they do!
I’m running a two-hour live workshop this Thursday called Python’s Plumbing • Dunder Methods and Python’s Hidden Interface. This is your last chance to join and I may not run this workshop again for a while.
This workshop is the first of three in the Python Behind the Scenes series. The other two workshops in the series are:
#2 • Pythonic Iteration: Iterables, Iterators,
itertools#3 • To Inherit or Not? Inheritance, Composition, Abstract Base Classes, and Protocols
Join all three, or pick and choose:
Image by Pete Linforth from Pixabay
For more Python resources, you can also visit Real Python—you may even stumble on one of my own articles or courses there!
Also, are you interested in technical writing? You’d like to make your own writing more narrative, more engaging, more memorable? Have a look at Breaking the Rules.
And you can find out more about me at stephengruppetta.com
Further reading related to this article’s topic:




