Python Tips: Living In The Land Down "dunder"
If you don't get the music reference from that title, you probably didn't grow up in the 80's. Here is a link to a music video that will make sense of it.
I think your spellcheck failed
If you're referring to the word "dunder", no it really is spelled that way (if you're referring to my writing, I blame it all on spellchecker). A “dunder” method is one that has double underscores before and after its name. Hence, the name implies a shortened version of the words “double-underscores”: dunder. These are also referred to as “magic” methods.
So what?
As you find more and more ways to automate boring tasks using Python, your scripts will start to get more complex. Eventually, it will make sense for you to start using Classes. I won't get into what Classes are because then I'd have to explain Object Oriented Programming and that’s a lot to put into one post. Just know that Classes are a way for you to create your own custom types. And when you create a custom type you have to define operators for those types.
What do you mean operators?
Think of mathematical operations like addition, subtraction, multiplication, etc. You also have logical operations like equals, greater-than, less-than, etc. This is why dunder methods matter; Classes contain dunder methods which allow you to specify how a type should behave in an operation. This is called overloading.
How can overloading something be good?
Let's assume you have two variables in your script and you want to add them so you add this line in your script: a + b = c
. If a
and b
are integers, the addition is straightforward. But what if the two variables are actually strings or lists (arrays), how will Python know what "adding" those types really means? For two strings, the +
operator amounts to concatenation:
> a = "Forest"
> b = "Gump"
> c = a + b
> c
'ForestGump'
For lists, the +
also amounts to concatenating the two lists into a new one:
> d = [1, 2, 3]
> e = [4, 5, 6]
> f = d + e
> f
[1, 2, 3, 4, 5, 6]
How did Python know what to do? Because someone took the time to overload the __add__
dunder method in the str
and list
classes.
Alright, already. Give me an example.
Let's say you want to work with complex numbers in Python so you decide to create your own Class (you don't need to do this because Python includes a complex type but let's just just use this as an example). You want to be able to do three basic operations in your customer Class:
1) create a complex number
2) add two complex numbers
3) print a complex number
The first dunder method you will override is __init__
. The is the constructor method for a class which means this is where you define the default behavior when creating a new instance of your custom type. For this example, you can define the __init__
method to take two parameters (assumed to be the real and imaginary values) and create a new object of ComplexNumber type.
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
And since this is a new type, you also need to define the __add__
function so the Python interpreter knows what to do when it sees c = a + b
and a
and b
are both ComplexNumber objects. In this case, add the real and imaginary parts of the two addends and return the sum as a new complex number. Here is an example of what the code would look like:
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, other_complex_number):
real_part = self.real + other_complex_number.real
imaginary_part = self.imaginary + other_complex_number.imaginary
return ComplexNumber(real_part, imaginary_part)
Now let’s say you want to print the complex number c
that was the result of the addition operation. To do that, you need to override the __str__
method like this:
def __str__(self):
return f'real: {self.real}, imag: {self.imaginary}'
Now when the Python interpreter sees the line print(c)
it will know what to do. There are lots of other dunder methods and they differ according to the data type but I think you get idea. If you want to see what dunder methods are available for a specific data type, use the dir(<type>)
command at the Python interpreter command line. For example, the screenshot at the top of this post shows all the dunder methods for the str
data type.
Conclusion
Don't freak out when you hear someone use the term "dunder method". Just nod your head with full confidence that you now know what they are and, when the time comes, you will know how to use them. Happy scripting.