Welcome back. So, supposing we have our point class from before,
and we have a constructor _init_ and
_str_ to represent how we represent any instance as a string.
Now, suppose that we have two points as well,
point one at negative five and 10 and point two at 15 and 20.
So, suppose that we wanted the ability to be able to add points together.
So, for example, if we take point one plus point two,
then we might get something like point one, two.
We might want to add points together by adding their x-coordinates.
So, negative five plus 15 would give us 10 and then adding our y-coordinate.
So, 10 plus 20 would give us 30.
So, our new point P, one,
two would be at 10 and 30.
So, if we write in our code, just to say,
print out the value of P1 plus P2,
then we're going to get an error saying that we can't add points.
In other words, when we tell Python that we want to add these two point instances,
then Python doesn't know how to actually take these instances and
produce something that represents their values added together.
But we can override a method called
_add_ that will tell Python how to actually add two points together.
So, if we say, def _add_.
So, add is going to take in self,
but it's also going to take in what we're expecting to add to self.
So, I'll call this otherPoint.
So, for example, when we print out the value of P1 plus P2,
then P1 gets passed in for self and P2 gets passed in for otherPoint.
Now, if we want to take the x coordinates and add them together and
then take the y coordinates and add them together and return a new point,
then what we can say is,
we want to return a new point whose x is equal to self.x,
plus otherPoint.x, and whose y is equal to self.y plus otherPoint.y.
Now, as soon as we define this add method,
then when we print out the value of point one plus point two,
then we're going to see that we get a new point at 10 and 30.
Again, this is just 15 plus negative five gives us 10 and 10 plus 20 gives us 30.
Beyond add, there are several other methods that we can override.
So, for example, there is subtraction.
So, for example, if we wanted to be able to print out the value of P1 minus P2,
then we could define this subtraction method.
Suppose that we wanted this to be defined
as self.x minus otherPoint.x and self.y minus otherPoint.y,
then now we have the ability to quote unquote subtract two points together.
There are many other methods that we can override,
but they follow the same pattern as add,
sub, and they all start and end with underscores.
That's all for now, until next time.