Welcome back. So, let's suppose that we
declared this class to represent a point on an x, y plane.
So, again, our class is called point.
We have a constructor that takes in argument for x,
for y, and assigns two instance variables,
x and y, to whatever is passed into the constructor.
Here, we also have methods that get the x value and get the y value,
and we have an additional method that determines the distance from the origin.
So, this is, if the point is at x and y,
it's going to get this distance from the origin and return it as a plot.
So, let's suppose that we create a point at seven and six.
If we print out, what is the value of this point?
So, I say print p. Then Python
gives us back just information saying that this is a point object.
It has no information about where the point actually is.
So, for example, if I had a second point at eight and nine,
and printed out p2,
I have no way of telling that p and p2 are different points.
So, classes can have an optional method called the underscore STR.
So, def the underscore STR the underscore.
Just like every other method,
it accepts self as the first argument.
What this, the underscore STR method,
does is it tells Python how to represent that object when you actually print it out.
So, if I specify that STR should print out just point by returning the string point,
now when I print out both of my point instances,
you can see that I get point.
If I instead print out point 123,
then I am going to print out point 123.
So, whatever gets returned by this,
the underscore STR method,
is what gets printed out when we convert
this point object to a string in order to print it.
So, for points, we might want to have something,
like point and then maybe the x and y position.
So, what I might say is,
point at x and y by saying
point dot format and self dot x, self dot y.
Now, when I print out point p,
then I am going to get to point at seven and six because
here seven gets substituted for self dot x,
six gets substituted for self dot y,
and self dot x goes into the first slot,
self dot y goes into the second slot.
So, now I can tell both of these points apart,
and when I print out the instance,
then I actually get something useful for a point.
So, when you create a class,
you often want to set the underscore STR method in order to get
more readable and understandable things to
print out when you actually print out any particular instance.
That's all for now, until next time.