Welcome to Python, Meet the Dunders

A quick introduction to several of the __magic__ methods in python

Andrew Scott
The Startup

--

What is a Dunder?

“Dunder” method name is the common pronunciation for python’s built-in method names that start and end with double underscores. Since “Dunder” is easier to say than “double underscore”, the name stuck. These methods are also sometimes referred to as “Special Methods” or “Magic Methods”. However, the only thing magic or special about these methods is that they allow you to include built-in type behavior in your custom classes.

While we sometimes treat these methods as a special language feature, there’s really nothing special about them. An introductory understanding of how to use dunder methods, and which ones are available can help you create much more intuitive and easy to use classes.

In this post we’ll look at several of the dunder methods used for object creation, representation, and comparison. In later posts we will explore other dunder methods such as emulating containers, emulating numeric types, and more.

The Basics

If you’ve written any python you’ve almost definitely used at least one dunder method, whether you realized it or not.

__init__

--

--

Andrew Scott
The Startup

Maintainer @OchronaSec | PANW, ex Expanse, ex Tenable | Security & Automation | All views are my own... and awesome