Comparing and Combining Dice5:41 with Kenneth Love
Dice are great but we need to be able to find ones that fit certain criteria, like being greater than four. It'd also be handy to be able to add dice together. We can do both of these things using magic methods!
If you want to get a lot of magic method goodies easily, check out
attrs. It's a solid library and makes a lot of common usages much easier.
Or, to stick with the standard library, check out the docs for
functools.total_ordering. You need to define
__eq__ and then one of the other operations and Python will figure out the rest.
Before you ask
Yes, I could have done something like:
def __le__(self, other): return int(self) <= other
Either format (long or short) is fine and produces the same result.
That's a great basic dye class, but we need to be able to do a lot more with it. 0:00 We've already added our D6 class to always have six sided dice. 0:04 If we're playing a lot of dice games, though, we need the sum total of our dice. 0:07 We really want to be able to compare dice to each other, or to another number too, 0:10 so we can find all the dice that have a value over four, or something. 0:14 We can accomplish all of these tasks with a few magic methods. 0:18 Great, let's get to it. 0:21 Okay, I wanna start with our equality comparison. 0:23 We can do an equality comparison, so double equals, stuff like that, with just 0:26 one magic method, but I'd like to be able to compare with greater than, less than, 0:31 and all of their friends, that means we'll have to add a few more things in. 0:34 First, let's start with turning an instance of our die into an integer. 0:39 So, we'll say int self, and we'll return self.value. 0:43 Okay? 0:48 Because we always have an int there, simple, easy enough. 0:49 Okay, so now to do the equality stuff. 0:52 We need to define six different magic methods. 0:56 I like to do them in alternating pairs, so let's start with equals and not equals. 0:59 So def __eq__(self): return 1:03 int(self) == other, and I need to take other in here as well. 1:07 Okay, and then def __ne__ for not equals. 1:13 We take self and other, and let's just return not int(self) == other. 1:17 We can also do return int(self) != other. 1:26 Either one of those that you want to do. 1:31 All right? 1:37 Both of these, we turn this instance into an int, compare it to the other value. 1:38 I don't think we need to test this right off quick, 1:41 you can see what's going on here. 1:43 All right, so let's move on to greater than and less than. 1:44 So def __gt__, we take (self, other): and we return int(self) > other or 1:48 def __ lt__ for less than, return int self less than the other thing. 1:54 Again, turning our instance into an int and then comparing it to the other value. 2:00 And again, these are opposites of each other both in usage and 2:07 in their actual code, right? 2:13 We use greater than on one and we use less than on the other. 2:15 Okay one more pair to do which is the greater than or equal to and less than or 2:19 equal to. 2:23 So we define ge self and other. 2:25 Return int self greater than the other or int self is equal to the other. 2:30 And then def __le(self, other): return 2:37 int(self) < other or int(self) == other. 2:42 I kind of wish these were named gte and 2:48 lte instead of ge and le, but we can't always have everything we want, can we? 2:52 All right so let's test our operator methods. 2:59 All right let's get over here, we'll go into Python. 3:04 And say from dice import D6. 3:07 And we'll say d6 = D6(). 3:11 And let's see what our d6 is. 3:15 I get 6 to the 6, great, all right, so d6 is less than 2. 3:18 False, that's right, it's not less than 2. 3:24 Our d6 is less than or equal to 2. 3:26 Nope, okay. 3:29 What about our d6 is greater than 1, that's true. 3:32 Is our d6 not equal to 4, that's also true. 3:36 And finally is our d6 equal to 6. 3:39 It is. 3:42 Okay. 3:43 Awesome. 3:44 Now you can add these to any class that you create. 3:44 They don't have to be related to numbers. 3:47 They can be strings or whatever. 3:50 But since ours is based on a number let's add in the ability to add them together. 3:52 Now I don't wanna do this in like in place of addition. 3:57 Because, well we wouldn't ever implace increment the value of a die, 4:00 but I do want to have add and r add. 4:06 So let's come down here def __add__(self, 4:11 other), return int(self) + other and 4:17 def __radd__(self, other). 4:22 And we could again just return self + other, or 4:27 we can do what we've been doing and say int (self) + other. 4:31 Those are exactly the same code, but that's what we need right there, right? 4:38 So no matter which some of the plus sign our die is on, 4:42 it should just add its value to the other objects value. 4:44 Okay. 4:48 So, we've got time to do one last test on this. 4:48 And then we'll move on to something more interesting. 4:52 From dice import d6. 4:56 And let's say d1 = D6 () and d2 = D6 (). 4:59 Int(d1) is 6 and int(d2) Is 4, 5:04 d1 + d2 is 10, d1 + 12, cuz I got a bonus, is 18. 5:10 And it works, so that's great. 5:15 Works just like we want it to, 5:18 and now I think we can do a lot more interesting things with our dice. 5:19 It can be a bit of a pain to declare all of those comparison methods. 5:23 If you think you need all of them, check out the ATTR's or 5:27 Atters package, which I've linked in the teacher's notes. 5:30 It makes things a bit easier at the expense of having another package to 5:33 install and use. 5:35 All right, let's look at creating a class to auto roll a set of dice for us. 5:37
You need to sign up for Treehouse in order to download course files.Sign up