Welcome back.
So you've already learned that functions and
methods can return any kind of value as their return value.
But one thing that's worth noting is that they can specifically return
other instances.
So let's suppose that we have this Point class.
And this Point class has a constructor, and
the constructor sets two instance variables for x and y.
And we have methods getX to return the x instance variable,
getY to return the y instance variable.
And then distancefromOrigin to return how far x and y are from the origin.
Now, suppose that we wanted to define a new method here.
Suppose that we wanted the ability to accept two points and
return a new point that was halfway between the two.
So for example, here I have point p, and I have point q.
And let's say that we wanted a function that would return a new point
that was halfway between point p and point q.
And let's call that function halfway, and let's make it a method on every point.
So for example, if I say p.halfway when
called with q, then this should return
a new Point that is halfway between p and q.
So if I call this midway, so I'll say mid = p.halfway(q),
then I want this to return a new point.
In this case, the x should be halfway between 3 and 5, so the x should be 4.
And the y should be halfway between 4 and 12, so the y should be 8.
So let's define that method.
So I'm going to say def halfway, Like every method, it accepts self.
And then I have a target.
And we want to return a new point that's halfway between self and target, so
I'll say the x.
So the mid x is equal to the average between self.x and target.x.
So self.x + target.x divided by 2.
Mid y = self.y + target.y divided by 2.
And I can return a new point, return Point whose x is mx and whose y is my.
And now if I print out what the value of mid is, And
run my code, Then I'll see that I get x = 4,
which is halfway between 3 and 5.
And I'll see that y is 8, which is halfway between 4 and 12.
And again, note that mid is a new instance of my Point class.
So I can do things like print out mid.getX,
and I can print out mid.getY.
And I'll get 4 and 8 respectively for those values.
That's all for now, until next time.