Member-only story
Removing Dunder Names from Python’s dir() Output
In this blog post, we will explore a common Python programming problem: how to remove dunder names from the output of the dir()
function. We will discuss what dunder names are, and then walk through each step of the solution, explaining the how and the why in simple terms.
What is a Dunder Name in Python?
In Python, a “dunder” name is short for “double underscore” name. These are special methods that have a double underscore prefix and suffix, such as __init__
, __str__
, and __add__
.
Dunder names are used to define how objects of a class behave in certain situations. For example, the __add__
method is called when you use the +
operator on objects of that class.
The Problem: Removing Dunder Names from dir() Output
The dir() function in Python is a built-in function that returns a list of names in the current local scope or the attributes of an object. When you call dir() on an object, it often includes dunder names, which can clutter the output and make it harder to see the relevant attributes and methods.