1 00:00:00,640 --> 00:00:04,440 That's a great basic die class, but we need to be able to do a lot more with it. 2 00:00:04,440 --> 00:00:07,940 We've already added our D6 class to always have six sided dice. 3 00:00:07,940 --> 00:00:10,850 If we're playing a lot of dice games, though, we need the sum total of our dice. 4 00:00:10,850 --> 00:00:14,770 We really want to be able to compare dice to each other, or to another number too, 5 00:00:14,770 --> 00:00:18,160 so we can find all the dice that have a value over four, or something. 6 00:00:18,160 --> 00:00:21,220 We can accomplish all of these tasks with a few magic methods. 7 00:00:21,220 --> 00:00:22,100 Great, let's get to it. 8 00:00:23,860 --> 00:00:26,800 Okay, I wanna start with our equality comparison. 9 00:00:26,800 --> 00:00:31,280 We can do an equality comparison, so double equals, stuff like that, with just 10 00:00:31,280 --> 00:00:34,900 one magic method, but I'd like to be able to compare with greater than, less than, 11 00:00:34,900 --> 00:00:39,010 and all of their friends, that means we'll have to add a few more things in. 12 00:00:39,010 --> 00:00:43,770 First, let's start with turning an instance of our die into an integer. 13 00:00:43,770 --> 00:00:48,240 So, we'll say __int__ self, and we'll return self.value. 14 00:00:48,240 --> 00:00:49,110 Okay? 15 00:00:49,110 --> 00:00:52,310 Because we always have an int there, simple, easy enough. 16 00:00:52,310 --> 00:00:56,300 Okay, so now to do the equality stuff. 17 00:00:56,300 --> 00:00:59,500 We need to define six different magic methods. 18 00:00:59,500 --> 00:01:03,304 I like to do them in alternating pairs, so let's start with equals and not equals. 19 00:01:03,304 --> 00:01:07,727 So def __eq__(self): return 20 00:01:07,727 --> 00:01:12,700 int(self) == other, and I need to take other in here as well. 21 00:01:13,730 --> 00:01:17,990 Okay, and then def __ne__ for not equals. 22 00:01:17,990 --> 00:01:26,292 We take self and other, and let's just return not int(self) == other. 23 00:01:26,292 --> 00:01:31,014 We can also do return __int__(self)!= other. 24 00:01:31,014 --> 00:01:35,620 Either one of those that you want to do. 25 00:01:37,160 --> 00:01:38,400 All right? 26 00:01:38,400 --> 00:01:41,290 Both of these, we turn this instance into an int, compare it to the other value. 27 00:01:41,290 --> 00:01:43,750 I don't think we need to test this right off quick, 28 00:01:43,750 --> 00:01:44,860 you can see what's going on here. 29 00:01:44,860 --> 00:01:48,313 All right, so let's move on to greater than and less than. 30 00:01:48,313 --> 00:01:54,895 So def __gt__, we take (self, other): and we return int(self) > other or 31 00:01:54,895 --> 00:02:00,659 def __ lt__ for less than, return int self less than the other thing. 32 00:02:00,659 --> 00:02:07,742 Again, turning our instance into an int and then comparing it to the other value. 33 00:02:07,742 --> 00:02:13,046 And again, these are opposites of each other both in usage and 34 00:02:13,046 --> 00:02:15,700 in their actual code, right? 35 00:02:15,700 --> 00:02:19,279 We use greater than on one and we use less than on the other. 36 00:02:19,279 --> 00:02:23,833 Okay one more pair to do which is the greater than or equal to and less than or 37 00:02:23,833 --> 00:02:24,500 equal to. 38 00:02:25,570 --> 00:02:30,500 So we define __ge__ self and other. 39 00:02:30,500 --> 00:02:37,442 Return int self greater than the other or int self is equal to the other. 40 00:02:37,442 --> 00:02:42,783 And then def __le__(self, other): return 41 00:02:42,783 --> 00:02:48,220 int(self) < other or int(self) == other. 42 00:02:48,220 --> 00:02:52,170 I kind of wish these were named gte and 43 00:02:52,170 --> 00:02:57,920 lte instead of ge and le, but we can't always have everything we want, can we? 44 00:02:59,000 --> 00:03:02,340 All right so let's test our operator methods. 45 00:03:04,020 --> 00:03:07,990 All right let's get over here, we'll go into Python. 46 00:03:07,990 --> 00:03:11,360 And say from dice import D6. 47 00:03:11,360 --> 00:03:15,110 And we'll say d6 = D6(). 48 00:03:15,110 --> 00:03:17,080 And let's see what our d6 is. 49 00:03:18,790 --> 00:03:24,090 I get 6 to the 6, great, all right, so d6 is less than 2. 50 00:03:24,090 --> 00:03:26,850 False, that's right, it's not less than 2. 51 00:03:26,850 --> 00:03:29,700 Our d6 is less than or equal to 2. 52 00:03:29,700 --> 00:03:32,700 Nope, okay. 53 00:03:32,700 --> 00:03:36,540 What about our d6 is greater than 1, that's true. 54 00:03:36,540 --> 00:03:39,640 Is our d6 not equal to 4, that's also true. 55 00:03:39,640 --> 00:03:42,760 And finally is our d6 equal to 6. 56 00:03:42,760 --> 00:03:43,650 It is. 57 00:03:43,650 --> 00:03:44,180 Okay. 58 00:03:44,180 --> 00:03:44,930 Awesome. 59 00:03:44,930 --> 00:03:47,450 Now you can add these to any class that you create. 60 00:03:47,450 --> 00:03:50,290 They don't have to be related to numbers. 61 00:03:50,290 --> 00:03:52,680 They can be strings or whatever. 62 00:03:52,680 --> 00:03:57,460 But since ours is based on a number let's add in the ability to add them together. 63 00:03:57,460 --> 00:04:00,620 Now I don't wanna do this in like in place of addition. 64 00:04:00,620 --> 00:04:06,590 Because, well we wouldn't ever in place increment the value of a die, 65 00:04:06,590 --> 00:04:11,750 but I do want to have __add__ and __radd__. 66 00:04:11,750 --> 00:04:17,495 So let's come down here def __add__(self, 67 00:04:17,495 --> 00:04:22,420 other), return int(self) + other and 68 00:04:22,420 --> 00:04:27,670 def __radd__(self, other). 69 00:04:27,670 --> 00:04:31,890 And we could again just return self + other, or 70 00:04:31,890 --> 00:04:36,920 we can do what we've been doing and say int (self) + other. 71 00:04:38,290 --> 00:04:42,730 Those are exactly the same code, but that's what we need right there, right? 72 00:04:42,730 --> 00:04:44,970 So no matter which some of the plus sign our die is on, 73 00:04:44,970 --> 00:04:48,430 it should just add its value to the other object's value. 74 00:04:48,430 --> 00:04:48,980 Okay. 75 00:04:48,980 --> 00:04:52,870 So, we've got time to do one last test on this. 76 00:04:52,870 --> 00:04:55,170 And then we'll move on to something more interesting. 77 00:04:56,590 --> 00:04:59,480 From dice import D6. 78 00:04:59,480 --> 00:05:04,881 And let's say d1 = D6 () and d2 = D6 (). 79 00:05:04,881 --> 00:05:10,320 int(d1) is 6 and int(d2) Is 4, 80 00:05:10,320 --> 00:05:15,400 d1 + d2 is 10, d1 + 12, cuz I got a bonus, is 18. 81 00:05:15,400 --> 00:05:18,390 And it works, so that's great. 82 00:05:18,390 --> 00:05:19,500 Works just like we want it to, 83 00:05:19,500 --> 00:05:22,470 and now I think we can do a lot more interesting things with our dice. 84 00:05:23,860 --> 00:05:27,040 It can be a bit of a pain to declare all of those comparison methods. 85 00:05:27,040 --> 00:05:30,440 If you think you need all of them, check out the attrs or 86 00:05:30,440 --> 00:05:33,240 Atters package, which I've linked in the teacher's notes. 87 00:05:33,240 --> 00:05:35,980 It makes things a bit easier at the expense of having another package to 88 00:05:35,980 --> 00:05:37,300 install and use. 89 00:05:37,300 --> 00:05:40,510 All right, let's look at creating a class to auto roll a set of dice for us.