1 00:00:07,910 --> 00:00:12,975 Welcome back. So, supposing we have our point class from before, 2 00:00:12,975 --> 00:00:17,140 and we have a constructor _init_ and 3 00:00:17,140 --> 00:00:23,370 _str_ to represent how we represent any instance as a string. 4 00:00:23,370 --> 00:00:25,905 Now, suppose that we have two points as well, 5 00:00:25,905 --> 00:00:31,215 point one at negative five and 10 and point two at 15 and 20. 6 00:00:31,215 --> 00:00:36,175 So, suppose that we wanted the ability to be able to add points together. 7 00:00:36,175 --> 00:00:41,695 So, for example, if we take point one plus point two, 8 00:00:41,695 --> 00:00:46,160 then we might get something like point one, two. 9 00:00:46,160 --> 00:00:50,660 We might want to add points together by adding their x-coordinates. 10 00:00:50,660 --> 00:00:55,520 So, negative five plus 15 would give us 10 and then adding our y-coordinate. 11 00:00:55,520 --> 00:00:58,860 So, 10 plus 20 would give us 30. 12 00:00:58,860 --> 00:01:00,930 So, our new point P, one, 13 00:01:00,930 --> 00:01:03,705 two would be at 10 and 30. 14 00:01:03,705 --> 00:01:08,060 So, if we write in our code, just to say, 15 00:01:08,060 --> 00:01:12,840 print out the value of P1 plus P2, 16 00:01:12,840 --> 00:01:17,405 then we're going to get an error saying that we can't add points. 17 00:01:17,405 --> 00:01:21,935 In other words, when we tell Python that we want to add these two point instances, 18 00:01:21,935 --> 00:01:25,730 then Python doesn't know how to actually take these instances and 19 00:01:25,730 --> 00:01:29,885 produce something that represents their values added together. 20 00:01:29,885 --> 00:01:33,355 But we can override a method called 21 00:01:33,355 --> 00:01:38,765 _add_ that will tell Python how to actually add two points together. 22 00:01:38,765 --> 00:01:45,960 So, if we say, def _add_. 23 00:01:45,960 --> 00:01:48,585 So, add is going to take in self, 24 00:01:48,585 --> 00:01:53,720 but it's also going to take in what we're expecting to add to self. 25 00:01:53,720 --> 00:01:56,340 So, I'll call this otherPoint. 26 00:01:58,250 --> 00:02:03,675 So, for example, when we print out the value of P1 plus P2, 27 00:02:03,675 --> 00:02:11,265 then P1 gets passed in for self and P2 gets passed in for otherPoint. 28 00:02:11,265 --> 00:02:15,140 Now, if we want to take the x coordinates and add them together and 29 00:02:15,140 --> 00:02:18,670 then take the y coordinates and add them together and return a new point, 30 00:02:18,670 --> 00:02:21,010 then what we can say is, 31 00:02:21,010 --> 00:02:28,605 we want to return a new point whose x is equal to self.x, 32 00:02:28,605 --> 00:02:38,840 plus otherPoint.x, and whose y is equal to self.y plus otherPoint.y. 33 00:02:40,090 --> 00:02:43,990 Now, as soon as we define this add method, 34 00:02:43,990 --> 00:02:47,260 then when we print out the value of point one plus point two, 35 00:02:47,260 --> 00:02:51,280 then we're going to see that we get a new point at 10 and 30. 36 00:02:51,280 --> 00:02:59,625 Again, this is just 15 plus negative five gives us 10 and 10 plus 20 gives us 30. 37 00:02:59,625 --> 00:03:03,400 Beyond add, there are several other methods that we can override. 38 00:03:03,400 --> 00:03:07,160 So, for example, there is subtraction. 39 00:03:09,620 --> 00:03:17,055 So, for example, if we wanted to be able to print out the value of P1 minus P2, 40 00:03:17,055 --> 00:03:20,795 then we could define this subtraction method. 41 00:03:20,795 --> 00:03:23,810 Suppose that we wanted this to be defined 42 00:03:23,810 --> 00:03:32,850 as self.x minus otherPoint.x and self.y minus otherPoint.y, 43 00:03:32,850 --> 00:03:38,285 then now we have the ability to quote unquote subtract two points together. 44 00:03:38,285 --> 00:03:40,835 There are many other methods that we can override, 45 00:03:40,835 --> 00:03:43,550 but they follow the same pattern as add, 46 00:03:43,550 --> 00:03:47,465 sub, and they all start and end with underscores. 47 00:03:47,465 --> 00:03:50,700 That's all for now, until next time.