Learning About Dunder Methods
00:00 In this lesson, you will be learning about Python’s special methods, also called dunder methods or magic methods. Now, why are they called dunder and magic?
00:11 Well, dunder stands for double underscore. So we will have two leading and two trailing underscores. And why magic? Well, they’re called magic because Python calls a dunder method automatically.
00:26
For example, when you call the built-in len()
function with a list object as an argument, Python calls list.__len__()
under the hood to retrieve the list’s length.
00:39
So the __len__()
it’s difficult to pronounce. That is your dunder method. You can see there are two leading and two trailing underscores. You can customize the behavior of those dunder methods and we will see an example of that in a second.
00:59
But there is a warning from PEP 8 in the section descriptive naming styles. It mentions that magic objects or attributes that live in user-controlled namespaces, such as __init__
, __import__
or __file__
, never invent such names, only use them as documented.
01:18
What does that mean? It means that the dunder syntax is reserved for special methods and attributes that are built into Python. You’ve seen the __init__()
method that was used to create class objects.
01:30 That’s a special method that is built into Python, you didn’t invent that yourself. It also means that you shouldn’t start creating your own dunder methods, and that’s because you don’t want to create a conflict with existing dunder methods that are built into Python already and that maybe you just didn’t know existed.
01:50
You will work through two examples. You’ll revisit the str
, the string class example, and then you will look at the __len__()
example.
02:02
So if you would please switch to your REPL, or your terminal, if you are in your terminal, then please start your REPL by typing python
or python3
.
02:12
And in here, let’s revisit the string example. So if you type dir(str)
02:18
then you’ll remember this from before. This is where we see all the objects in the public interface and we have tons of public ones: find
, format
, format_map
, index
, many of those you will recognize.
02:31
But then there are all these dunder methods. The long line here, that’s actually two underscores, two underscores, two underscores. You might recognize some of them: __getattribute__()
__getitem__()
, and so on.
02:43
So that’s the example for the string class. Let’s move on to the example where you will build your own class. So I like to have the full screen, so I press Control + L to clear my REPL. I would like you to create a ShoppingCart
class.
02:59
And you have an online shop. There is a shopping cart in there, and you need this class to support the len()
function, and this len()
function should return the number of items in the cart.
03:12
So let’s create the class ShoppingCart
,
03:16
and then we need four spaces for indentation 1, 2, 3, 4. And then we create the __init__()
method, which is a dunder method you will be familiar with.
03:26
And then we need eight spaces, 1, 2, 3, 4, 1, 2, 3, 4. And we’ll create .products
attribute. And at the moment, that is just an empty list. We’ll also need a method to add products to our cart, so four spaces, 1, 2, 3, 4.
03:45
And this method, we’ll call it add_product()
.
03:49
And that takes product
as an input. So this is the product you will be adding to your cart.
03:55
And then we’ll need eight spaces, 1, 2, 3, 4, 1, 2, 3, 4. And what we will do is we will then append the product to our .products
list. So self.products.append()
, what will we append?
04:11
Well, we will append the product
. That was the input parameter for this method here. So self.products.append(product)
.
04:20
Now we need another method that allows us to see the products in our cart. So 1, 2, 3, 4. This method is called get_products()
.
04:31
It doesn’t take any input because it just reads what is in the cart. And here. So you need eight, 1, 2, 3, 4, 1, 2, 3, 4. You return, just return the attribute .products
, which is our list of products.
04:47
And this is all quite familiar territory. The next step though is where we will be introducing our dunder method. So press Enter, and here we create the __len__()
dunder method.
05:01
Press Enter, and then we need eight, 1, 2, 3, 4, 1, 2, 3, 4. And what we will return is the length of self.products
. So self.products
is a list, so we just take the length of this list and we will return that.
05:19 Okay, time to test this out. Press Enter twice. So you get back into your REPL with the three arrows. Let’s create a shopping cart, so let’s create an instance of our class.
05:30
So cart = ShoppingCart()
cart, right. And well, let’s add a product. So we’re shopping around. I want to add a product. So I will call the add_product()
method from the class.
05:45 And this method takes an input, which is the product, and I’m going to add, let’s say a keyboard.
05:54 Okay, no error messages. That is good. Let’s try something else. What else am I buying?
06:02
Well, if I’m buying a keyboard, I might as well buy a mouse, and let’s buy maybe a monitor as well. Alright, so cart.add_product()
06:14 and do a monitor. So I have bought three products. Well, I’ve added three products to my cart. Let’s find out the length of the cart. And hey, presto, it works.
06:27
So there are three items in our cart. Now the thing about this is that we are applying the len()
function to a cart, which is a class instance.
06:38
Normally, the len()
function as we know it is applied to a list or some sort of iterable, not to a class instance, but because we implemented the __len__()
method the dunder len method here in our class, it has become a method of that class.
06:54
So that works. Let’s see if we can test the other method as well. So cart.get_products()
07:04
Yep. And this is a mistake I make all the time. So I have not typed the two brackets. So I have just asked for get_products
that is indeed a method.
07:15
So this is what Python is telling me, that this is a method of ShoppingCart
, of the ShoppingCart
object. So if I actually want to see the products, which is what my intention was, I of course need to not forget brackets.
07:29
So get_products()
and execute that method. And there you go. So our cart has three products, the keyboard, the mouse, and the monitor.
07:41 And that’s it for dunder methods. In the next lesson, you learn about trailing underscores. See you there.
Become a Member to join the conversation.