1 00:00:07,910 --> 00:00:11,860 Welcome back. So, let's suppose that we 2 00:00:11,860 --> 00:00:15,130 declared this class to represent a point on an x, y plane. 3 00:00:15,130 --> 00:00:17,200 So, again, our class is called point. 4 00:00:17,200 --> 00:00:21,525 We have a constructor that takes in argument for x, 5 00:00:21,525 --> 00:00:24,750 for y, and assigns two instance variables, 6 00:00:24,750 --> 00:00:28,270 x and y, to whatever is passed into the constructor. 7 00:00:28,270 --> 00:00:34,295 Here, we also have methods that get the x value and get the y value, 8 00:00:34,295 --> 00:00:38,495 and we have an additional method that determines the distance from the origin. 9 00:00:38,495 --> 00:00:43,200 So, this is, if the point is at x and y, 10 00:00:43,200 --> 00:00:48,190 it's going to get this distance from the origin and return it as a plot. 11 00:00:48,190 --> 00:00:52,175 So, let's suppose that we create a point at seven and six. 12 00:00:52,175 --> 00:00:54,695 If we print out, what is the value of this point? 13 00:00:54,695 --> 00:00:58,520 So, I say print p. Then Python 14 00:00:58,520 --> 00:01:02,675 gives us back just information saying that this is a point object. 15 00:01:02,675 --> 00:01:05,825 It has no information about where the point actually is. 16 00:01:05,825 --> 00:01:12,995 So, for example, if I had a second point at eight and nine, 17 00:01:12,995 --> 00:01:17,010 and printed out p2, 18 00:01:17,090 --> 00:01:22,485 I have no way of telling that p and p2 are different points. 19 00:01:22,485 --> 00:01:27,855 So, classes can have an optional method called the underscore STR. 20 00:01:27,855 --> 00:01:33,510 So, def the underscore STR the underscore. 21 00:01:33,510 --> 00:01:35,265 Just like every other method, 22 00:01:35,265 --> 00:01:39,420 it accepts self as the first argument. 23 00:01:39,420 --> 00:01:42,180 What this, the underscore STR method, 24 00:01:42,180 --> 00:01:47,075 does is it tells Python how to represent that object when you actually print it out. 25 00:01:47,075 --> 00:01:54,775 So, if I specify that STR should print out just point by returning the string point, 26 00:01:54,775 --> 00:01:58,465 now when I print out both of my point instances, 27 00:01:58,465 --> 00:02:00,395 you can see that I get point. 28 00:02:00,395 --> 00:02:05,020 If I instead print out point 123, 29 00:02:05,020 --> 00:02:07,500 then I am going to print out point 123. 30 00:02:07,500 --> 00:02:10,620 So, whatever gets returned by this, 31 00:02:10,620 --> 00:02:12,770 the underscore STR method, 32 00:02:12,770 --> 00:02:15,245 is what gets printed out when we convert 33 00:02:15,245 --> 00:02:18,455 this point object to a string in order to print it. 34 00:02:18,455 --> 00:02:21,720 So, for points, we might want to have something, 35 00:02:21,720 --> 00:02:24,920 like point and then maybe the x and y position. 36 00:02:24,920 --> 00:02:26,825 So, what I might say is, 37 00:02:26,825 --> 00:02:30,860 point at x and y by saying 38 00:02:30,860 --> 00:02:39,150 point dot format and self dot x, self dot y. 39 00:02:39,150 --> 00:02:41,730 Now, when I print out point p, 40 00:02:41,730 --> 00:02:44,790 then I am going to get to point at seven and six because 41 00:02:44,790 --> 00:02:49,785 here seven gets substituted for self dot x, 42 00:02:49,785 --> 00:02:53,505 six gets substituted for self dot y, 43 00:02:53,505 --> 00:02:56,100 and self dot x goes into the first slot, 44 00:02:56,100 --> 00:02:58,875 self dot y goes into the second slot. 45 00:02:58,875 --> 00:03:01,950 So, now I can tell both of these points apart, 46 00:03:01,950 --> 00:03:04,380 and when I print out the instance, 47 00:03:04,380 --> 00:03:08,040 then I actually get something useful for a point. 48 00:03:08,040 --> 00:03:09,060 So, when you create a class, 49 00:03:09,060 --> 00:03:12,620 you often want to set the underscore STR method in order to get 50 00:03:12,620 --> 00:03:15,320 more readable and understandable things to 51 00:03:15,320 --> 00:03:18,875 print out when you actually print out any particular instance. 52 00:03:18,875 --> 00:03:21,960 That's all for now, until next time.