Exploring Dunder Methods in Action
00:00
Letâs take a look at that, and letâs take a look at yet another dunder method with IDLE. On the right here, we have a file defining a Point class, and on the left, we have the IDLE Shell.
00:14
Right now if I save with Control + S and F5 to run, we donât get any output. So the class is being defined without any errors. So letâs instantiate a couple instances of this class. Letâs look at origin = Point() weâll call it 0, 0, and weâll say target and weâll use the same code to instantiate it.
00:39
So to us, to humans, this seems like theyâre both the same point, right? Theyâre both pointing at the same point in space: 0, 0. But if we try and run origin == target, and we Control + S to save and F5 to run, youâll see that that gives us False.
01:00
And thatâs because origin and target are two different instances of the class. They have different memory addresses. They live separately in memory in Python, and therefore if you just compare them directly, they are not equal to each other, even though to us it seems like they are the same because the values within them, the x and y values, 0, 0, conceptually for humans point to the same thing, so they should be equal.
01:30
So the way we would properly check with that is if origin.x == target.x and then also andâletâs format this so it fits nicely on-screenâand then origin.y == target.y.
01:54
If we now Control + S to save this and F5 to run that, weâll see that itâs True. And now this is the check that we really want to make, whether origin.x is the same as target.x and origin.y is the same as target.y, but thereâs quite a lot of stuff to type out and to remember every time we want to compare a point. Maybe we want to compare lots of points in our program.
02:17
So ideally we just want the original version, origin == target, without any referencing of .x and .y just to work. We could make a custom method, but we can use another special dunder method called def __eq__(), of course stands for equals.
02:39
The way this one works is that it takes two argumentsâself and other typically is what theyâre called, self referring to the instance on the left-hand side.
02:50 So weâre going to try and make this work.
02:55
print(origin == target) and then we will add in the check that we did before, other.x. We could assume that theyâre both instances of the same class.
03:13 And weâll return that directly.
03:17 And weâll just format that so it fits nicely on this reduced screen.
03:24
So you can do a check. You could say if this conditional check and then return True else False. But returning just this expression has the same effect.
03:35
So if this is actually true, then it will return True, and if itâs not true, then itâll return False. So the way this works is that whenever you have an instance of Point and you use the == (equals to) operator, it will take the object on the left, and that will be assigned to selfâso here, self.x and self.yâand then the object on the right will be assigned to other, so target becomes other.x, other.y.
04:05
So now when we run this code, Control + S, F5, youâll get True. So this is just one of the features that makes Python really great for object-oriented programming because you have a whole range of these special dunder methods where you can override this operator, the == two operator, but also addition, subtraction, multiplication, greater than, less thanâyou can define the behavior for all of these, so then you can use operators with your objects and not have to define custom methods like origin.is_equal_to() and then have target, which you can do, but somehow just being able to use the operators seems neater.
04:51 And that is an example of using a special method with your classes.
Become a Member to join the conversation.
Get a Python Cheat Sheet (PDF) and learn the basics of Python, like working with data types, dictionaries, lists, and Python functions:

