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.