1 00:00:09,281 --> 00:00:10,538 Welcome back. 2 00:00:10,538 --> 00:00:12,480 So you've already learned that functions and 3 00:00:12,480 --> 00:00:16,500 methods can return any kind of value as their return value. 4 00:00:16,500 --> 00:00:19,880 But one thing that's worth noting is that they can specifically return 5 00:00:19,880 --> 00:00:21,460 other instances. 6 00:00:21,460 --> 00:00:24,170 So let's suppose that we have this Point class. 7 00:00:24,170 --> 00:00:27,057 And this Point class has a constructor, and 8 00:00:27,057 --> 00:00:30,811 the constructor sets two instance variables for x and y. 9 00:00:30,811 --> 00:00:35,261 And we have methods getX to return the x instance variable, 10 00:00:35,261 --> 00:00:38,788 getY to return the y instance variable. 11 00:00:38,788 --> 00:00:43,980 And then distancefromOrigin to return how far x and y are from the origin. 12 00:00:45,280 --> 00:00:48,730 Now, suppose that we wanted to define a new method here. 13 00:00:48,730 --> 00:00:54,710 Suppose that we wanted the ability to accept two points and 14 00:00:54,710 --> 00:00:58,030 return a new point that was halfway between the two. 15 00:00:58,030 --> 00:01:04,280 So for example, here I have point p, and I have point q. 16 00:01:04,280 --> 00:01:08,830 And let's say that we wanted a function that would return a new point 17 00:01:08,830 --> 00:01:12,250 that was halfway between point p and point q. 18 00:01:12,250 --> 00:01:16,480 And let's call that function halfway, and let's make it a method on every point. 19 00:01:16,480 --> 00:01:21,663 So for example, if I say p.halfway when 20 00:01:21,663 --> 00:01:26,695 called with q, then this should return 21 00:01:26,695 --> 00:01:32,190 a new Point that is halfway between p and q. 22 00:01:33,450 --> 00:01:39,030 So if I call this midway, so I'll say mid = p.halfway(q), 23 00:01:39,030 --> 00:01:42,040 then I want this to return a new point. 24 00:01:42,040 --> 00:01:47,470 In this case, the x should be halfway between 3 and 5, so the x should be 4. 25 00:01:47,470 --> 00:01:52,228 And the y should be halfway between 4 and 12, so the y should be 8. 26 00:01:52,228 --> 00:01:54,320 So let's define that method. 27 00:01:54,320 --> 00:02:01,560 So I'm going to say def halfway, Like every method, it accepts self. 28 00:02:01,560 --> 00:02:02,876 And then I have a target. 29 00:02:06,274 --> 00:02:10,562 And we want to return a new point that's halfway between self and target, so 30 00:02:10,562 --> 00:02:11,990 I'll say the x. 31 00:02:11,990 --> 00:02:18,245 So the mid x is equal to the average between self.x and target.x. 32 00:02:18,245 --> 00:02:24,511 So self.x + target.x divided by 2. 33 00:02:24,511 --> 00:02:31,490 Mid y = self.y + target.y divided by 2. 34 00:02:31,490 --> 00:02:39,510 And I can return a new point, return Point whose x is mx and whose y is my. 35 00:02:41,550 --> 00:02:47,131 And now if I print out what the value of mid is, And 36 00:02:47,131 --> 00:02:53,802 run my code, Then I'll see that I get x = 4, 37 00:02:53,802 --> 00:02:58,182 which is halfway between 3 and 5. 38 00:02:58,182 --> 00:03:02,631 And I'll see that y is 8, which is halfway between 4 and 12. 39 00:03:02,631 --> 00:03:07,050 And again, note that mid is a new instance of my Point class. 40 00:03:07,050 --> 00:03:13,409 So I can do things like print out mid.getX, 41 00:03:13,409 --> 00:03:18,390 and I can print out mid.getY. 42 00:03:18,390 --> 00:03:21,750 And I'll get 4 and 8 respectively for those values. 43 00:03:22,820 --> 00:03:24,680 That's all for now, until next time.