1 00:00:08,900 --> 00:00:12,375 Welcome back. So, we've already seen that 2 00:00:12,375 --> 00:00:15,885 every instance of a class can have its own instance variables. 3 00:00:15,885 --> 00:00:21,939 So for example, here we have a point class and inside of the constructor _init_, 4 00:00:21,939 --> 00:00:26,070 we can see that every point instance has instance variables for 5 00:00:26,070 --> 00:00:31,350 X and Y to represent the position of that particular point. 6 00:00:31,350 --> 00:00:38,685 Now, for example, here when we create a new instance of point with X equal 2, Y equal 3, 7 00:00:38,685 --> 00:00:44,045 then suppose that our point class is represented with this notation again, 8 00:00:44,045 --> 00:00:53,540 so this represents our point class and p1 is an instance of 9 00:00:53,540 --> 00:00:58,590 the point class that has instance variable X set 10 00:00:58,590 --> 00:01:04,545 to 2 and instance variable Y set to 3. 11 00:01:04,545 --> 00:01:08,415 Now, we also have another instance of our point class, 12 00:01:08,415 --> 00:01:14,095 so that's p2 and p2 13 00:01:14,095 --> 00:01:21,330 has X set to 3 and Y set to 12. 14 00:01:21,330 --> 00:01:27,120 All right, so we have p1 and we have p2. 15 00:01:33,950 --> 00:01:37,120 So, one thing that I want to note is that, 16 00:01:37,120 --> 00:01:41,890 instances have their own variables but classes can have their own class variables. 17 00:01:41,890 --> 00:01:46,495 They work just like instance variables except that they belong to the class itself. 18 00:01:46,495 --> 00:01:51,715 So for example, here the point class has its own class variable called printed_rep. 19 00:01:51,715 --> 00:01:55,015 So I'm going to write that inside of the class point, 20 00:01:55,015 --> 00:02:05,330 so I'll say printed_rep is set to a star. 21 00:02:06,590 --> 00:02:12,540 Now, the point class also has these methods which are also a form of class variables, 22 00:02:12,540 --> 00:02:19,755 so we have to _init_ and its value is 23 00:02:19,755 --> 00:02:28,030 some function and we 24 00:02:28,030 --> 00:02:32,990 have this graph method whose value is a different function. 25 00:02:43,240 --> 00:02:47,180 Now, whenever python searches for anything 26 00:02:47,180 --> 00:02:57,570 of the form instancename.instancevar, 27 00:02:59,570 --> 00:03:06,920 so for example, if we're doing p1.graph or p2.graph or 28 00:03:06,920 --> 00:03:17,920 even something like print p1.printed_rep or print p1.x, 29 00:03:17,920 --> 00:03:22,275 then the search order that the python evaluator goes in is, 30 00:03:22,275 --> 00:03:25,095 it first searches inside of the instance. 31 00:03:25,095 --> 00:03:26,455 So in this case, 32 00:03:26,455 --> 00:03:30,300 if we're searching for p1.x and then it searches inside a p1 33 00:03:30,300 --> 00:03:34,670 and it finds x there and it evaluates that to be 2. 34 00:03:34,670 --> 00:03:38,075 If it doesn't find an instance variable with the name 35 00:03:38,075 --> 00:03:43,055 x or in this case let's go with the example of p1.printed_rep, 36 00:03:43,055 --> 00:03:45,725 so if we're searching for p1.printed_rep, 37 00:03:45,725 --> 00:03:49,315 python first searches inside of the instance p1, 38 00:03:49,315 --> 00:03:51,180 it doesn't find it there, 39 00:03:51,180 --> 00:03:55,640 so then it searches inside of the point class and that's where it finds it. 40 00:03:55,640 --> 00:03:57,770 When we say p2.graph, 41 00:03:57,770 --> 00:04:00,940 python first searches inside of p2, 42 00:04:00,940 --> 00:04:07,490 it doesn't find it there and so it searches inside of the point class where it finds it. 43 00:04:07,490 --> 00:04:12,330 If we had something like print p1.z, 44 00:04:13,340 --> 00:04:16,830 then python would first search inside of p1, 45 00:04:16,830 --> 00:04:18,450 it wouldn't find z there. 46 00:04:18,450 --> 00:04:20,480 It wouldn't find z there. 47 00:04:20,480 --> 00:04:22,720 It would then search inside the point class, 48 00:04:22,720 --> 00:04:28,715 it wouldn't find z there either and so it would give us a run-time error. 49 00:04:28,715 --> 00:04:31,900 Now, the way that we define this point class, 50 00:04:31,900 --> 00:04:34,015 I erase these annotations, 51 00:04:34,015 --> 00:04:36,580 the way that we define the point class is that we have 52 00:04:36,580 --> 00:04:39,730 this graph method and what graph does is a kind 53 00:04:39,730 --> 00:04:46,750 of forms a text graph or a textual representation of a graph that contains this point. 54 00:04:46,750 --> 00:04:56,025 So, if I erase my annotations and I run my code, 55 00:04:56,025 --> 00:04:58,505 so here I create two instances of point, 56 00:04:58,505 --> 00:05:00,440 one at 2, 3 one at 3, 57 00:05:00,440 --> 00:05:05,750 12 and I call p1.graph and what that does is it 58 00:05:05,750 --> 00:05:11,465 creates a little textural representation where here you can see kind of an X axis, 59 00:05:11,465 --> 00:05:21,020 here you can see kind of a Y axis and I can see that my point p1 is at 2. 60 00:05:23,120 --> 00:05:33,960 So 2 and 3 and I can see that my point p2 is at 3 and 12. 61 00:05:33,960 --> 00:05:35,715 Inside of this representation of graph, 62 00:05:35,715 --> 00:05:38,325 I refer to self.printed_rep. 63 00:05:38,325 --> 00:05:40,305 So you'll note that here, 64 00:05:40,305 --> 00:05:44,660 printed_rep again is a class variable that's set to a star. 65 00:05:44,660 --> 00:05:49,225 If I change printed_rep to be let's say a lowercase x, 66 00:05:49,225 --> 00:05:51,540 then you'll see that printed_rep changes for 67 00:05:51,540 --> 00:05:56,435 both p1 and p2 when we actually called the graph method on both of them. 68 00:05:56,435 --> 00:06:00,710 So, that's an example of how classes can contain variables. 69 00:06:00,710 --> 00:06:03,290 So again, we have instance variables that belong to 70 00:06:03,290 --> 00:06:07,985 every particular instance and we have class variables which belong to a class. 71 00:06:07,985 --> 00:06:10,595 Here technically, there are three class variables, 72 00:06:10,595 --> 00:06:16,490 there's printed_rep, _init_ and graph that all belong to the point class. 73 00:06:16,490 --> 00:06:19,440 That's all for now until next time.