WEBVTT 1 00:00:01.030 --> 00:00:02.190 Hi and welcome back. 2 00:00:02.190 --> 00:00:04.220 In this video we are going to continue with our 3 00:00:04.220 --> 00:00:05.800 student class and we're going 4 00:00:05.800 --> 00:00:07.610 to look at the average function. 5 00:00:07.610 --> 00:00:09.500 So in the last video we discovered that 6 00:00:09.500 --> 00:00:13.250 when we call a class that creates a blank object 7 00:00:14.500 --> 00:00:17.530 and that gets passed to the Dunder init method as self 8 00:00:17.530 --> 00:00:20.540 and then we can modify that by creating 9 00:00:20.540 --> 00:00:24.990 new variables inside that blank object. 10 00:00:24.990 --> 00:00:29.523 That's how these things here end up with a name property. 11 00:00:30.710 --> 00:00:33.590 Lest, I just said property, and let's start calling 12 00:00:33.590 --> 00:00:35.410 things by what they are. 13 00:00:35.410 --> 00:00:37.890 When we define a variable inside an object, 14 00:00:37.890 --> 00:00:41.264 that's no longer a variable now it's called a property. 15 00:00:41.264 --> 00:00:42.723 A property of the object. 16 00:00:44.360 --> 00:00:46.470 Similarly when we define a function like this 17 00:00:46.470 --> 00:00:50.980 average function, inside a class that is no longer 18 00:00:50.980 --> 00:00:53.980 called a function, that is now called a method. 19 00:00:53.980 --> 00:00:56.890 Same thing it's just a different name in order for, 20 00:00:56.890 --> 00:00:58.630 when you talk about programming to be able to 21 00:00:58.630 --> 00:01:01.190 quickly differentiate between what you're talking about. 22 00:01:01.190 --> 00:01:05.330 So student one and student two, know that they are students. 23 00:01:05.330 --> 00:01:08.470 They are off type student, because that's the way 24 00:01:08.470 --> 00:01:11.473 this was created when we called the class. 25 00:01:12.410 --> 00:01:14.470 Therefore, because they're off type student 26 00:01:14.470 --> 00:01:15.870 they know that they have this 27 00:01:15.870 --> 00:01:18.190 average function inside of them. 28 00:01:18.190 --> 00:01:23.190 Now we can call student_one.average 29 00:01:24.130 --> 00:01:27.055 we're calling the average function which is inside 30 00:01:27.055 --> 00:01:28.960 of our student one. 31 00:01:28.960 --> 00:01:32.200 Now you can see that there's a self parameter here 32 00:01:32.200 --> 00:01:35.100 which we are clearly not fully filling. 33 00:01:35.100 --> 00:01:37.950 So what do you think is going to happen 34 00:01:37.950 --> 00:01:39.223 when we print this out? 35 00:01:40.264 --> 00:01:41.403 Let's run it! 36 00:01:42.560 --> 00:01:45.923 And, hmm, we get a value and a valued one at that. 37 00:01:47.410 --> 00:01:48.540 So why is that? 38 00:01:49.620 --> 00:01:54.620 Well Python is pretty smart and when you use an object 39 00:01:54.760 --> 00:01:58.730 to call a function of the class, it automatically 40 00:01:58.730 --> 00:02:02.943 populates self as the object that called that function. 41 00:02:04.300 --> 00:02:05.950 Okay, let me give you another example that you 42 00:02:05.950 --> 00:02:07.600 may understand a bit more easily. 43 00:02:08.500 --> 00:02:12.540 Let's call the function, sorry the method here in the class. 44 00:02:12.540 --> 00:02:15.280 The way that you'd sort of expect it to do that is by 45 00:02:15.280 --> 00:02:18.440 saying student, which is the class, dot to access 46 00:02:18.440 --> 00:02:20.892 something inside of it, average. 47 00:02:20.892 --> 00:02:21.830 Right? 48 00:02:21.830 --> 00:02:24.020 We've got the class, we access something inside of it 49 00:02:24.020 --> 00:02:26.483 and what we are accessing is the average function. 50 00:02:27.360 --> 00:02:32.201 However if we do this and we run it, now we see that 51 00:02:32.201 --> 00:02:35.200 it says, there's something not very readable, 52 00:02:35.200 --> 00:02:37.410 average, the average function is missing 53 00:02:37.410 --> 00:02:40.623 one required argument which is self. 54 00:02:41.820 --> 00:02:46.820 Hmm, so Python obviously cannot tell what object 55 00:02:47.040 --> 00:02:49.240 you want to get the average of, 56 00:02:49.240 --> 00:02:50.930 because you've not called it with an object, 57 00:02:50.930 --> 00:02:52.760 you've called it with the class which is the 58 00:02:52.760 --> 00:02:55.260 definition of an object. 59 00:02:55.260 --> 00:02:56.093 So what to do? 60 00:02:56.093 --> 00:02:58.890 Is to pass in a particular object, 61 00:02:58.890 --> 00:03:01.090 ooh not my student that's a dictionary. 62 00:03:01.090 --> 00:03:04.810 Student one, now when you call this student one 63 00:03:04.810 --> 00:03:08.560 will be self as normal in Python functions and you will 64 00:03:08.560 --> 00:03:11.097 return that so we can once again run it. 65 00:03:11.097 --> 00:03:13.513 And now you can see we get the correct value. 66 00:03:15.320 --> 00:03:18.510 When you do student_one.average Python in the background 67 00:03:18.510 --> 00:03:20.820 is doing this, so there's no magic going on 68 00:03:20.820 --> 00:03:23.560 whatsoever, it's just that in order to save you 69 00:03:23.560 --> 00:03:25.400 from typing a bunch of stuff, 70 00:03:25.400 --> 00:03:28.390 you can just call it on the student and that also 71 00:03:28.390 --> 00:03:30.680 sort of makes sense, because now you're getting the 72 00:03:30.680 --> 00:03:31.840 average of student one, 73 00:03:31.840 --> 00:03:33.910 you don't have to include the student class in there, 74 00:03:33.910 --> 00:03:37.240 you know, self gets automatically populated for you 75 00:03:37.240 --> 00:03:39.870 when you call any method, remember that safe function 76 00:03:39.870 --> 00:03:43.793 within a class, with the student as the caller. 77 00:03:44.780 --> 00:03:47.190 And the student here is the caller because 78 00:03:47.190 --> 00:03:50.100 that's where we're calling this function from. 79 00:03:50.100 --> 00:03:51.550 Okay that's what this called. 80 00:03:53.540 --> 00:03:57.283 Okay so let's think about this average method again. 81 00:03:58.390 --> 00:04:01.830 We've got student one being passed as self, 82 00:04:01.830 --> 00:04:05.140 and we must remember now that student one was this 83 00:04:05.140 --> 00:04:07.060 object that was created and emptied here, 84 00:04:07.060 --> 00:04:09.130 also called self but that's just a side effect 85 00:04:09.130 --> 00:04:11.600 of the naming conventions in Python. 86 00:04:11.600 --> 00:04:14.460 Student one is the self thing here and we know it has 87 00:04:14.460 --> 00:04:17.350 a name and it also has a grades. 88 00:04:17.350 --> 00:04:21.340 So when we access student_one.grades that is what 89 00:04:21.340 --> 00:04:23.253 we passed initially up here. 90 00:04:24.170 --> 00:04:26.640 And here we're also accessing student_one.grades 91 00:04:27.550 --> 00:04:30.320 however in order to make this a bit more generic, 92 00:04:30.320 --> 00:04:33.261 we're not using average only on student one, 93 00:04:33.261 --> 00:04:36.985 but on self because we define self.grades, 94 00:04:36.985 --> 00:04:40.670 we know that we have self.grades available 95 00:04:40.670 --> 00:04:44.150 to use in the average function so we can use self.grades 96 00:04:44.150 --> 00:04:47.120 and self.grades and it depends on which student object 97 00:04:47.120 --> 00:04:50.240 we call it on that it's gonna use either these grades 98 00:04:50.240 --> 00:04:52.880 if we call it with student one or these grades 99 00:04:52.880 --> 00:04:54.430 if we call it with student two. 100 00:04:55.420 --> 00:04:58.970 We can re-write this function entirely, by the way, 101 00:04:58.970 --> 00:05:03.970 down here... while making it accept a student 102 00:05:05.020 --> 00:05:07.890 and it's gonna return the sum of student.grades 103 00:05:07.890 --> 00:05:09.783 and the length of student.grades. 104 00:05:10.680 --> 00:05:15.420 Okay, we're gonna print student_one.grades 105 00:05:18.330 --> 00:05:21.770 and then we're gonna call the average function call 106 00:05:21.770 --> 00:05:23.493 of student one. 107 00:05:25.780 --> 00:05:27.090 The first thing we're gonna do is print out the 108 00:05:27.090 --> 00:05:31.707 student's grades which we know should be 70, 88, 90, and 99. 109 00:05:33.740 --> 00:05:36.075 And indeed there we get them here, 110 00:05:36.075 --> 00:05:39.730 then we define this function that takes in a student object, 111 00:05:39.730 --> 00:05:41.870 returns the sum of it's grades property 112 00:05:41.870 --> 00:05:43.490 the one that we just printed up here, 113 00:05:43.490 --> 00:05:46.560 and the length and then divides one by another. 114 00:05:46.560 --> 00:05:50.370 Then we call that function with our student one object 115 00:05:50.370 --> 00:05:52.053 passed in as an argument. 116 00:05:54.090 --> 00:05:58.060 So student one, this is student one, this is student one, 117 00:05:58.060 --> 00:05:59.293 student_one.grades is the same thing we just 118 00:05:59.293 --> 00:06:01.120 printed up here. 119 00:06:01.120 --> 00:06:04.270 And student_one.grades is the same things we printed up here 120 00:06:04.270 --> 00:06:07.410 and then that is how the average gets calculated. 121 00:06:07.410 --> 00:06:11.570 Again, just a recap, when you call student_one.average 122 00:06:11.570 --> 00:06:13.970 what's happening in the background 123 00:06:13.970 --> 00:06:18.136 is student.average(student_one) 124 00:06:18.136 --> 00:06:20.693 student one is being passed as self. 125 00:06:21.970 --> 00:06:24.410 Same function we just defined only the argument 126 00:06:24.410 --> 00:06:26.340 which is really the parameter is called self. 127 00:06:26.340 --> 00:06:29.453 Again you could call this student if you wanted. 128 00:06:30.690 --> 00:06:32.830 But it is not common to do that. 129 00:06:32.830 --> 00:06:35.960 Normally in methods the first parameter 130 00:06:35.960 --> 00:06:37.791 is always called self because it refers 131 00:06:37.791 --> 00:06:40.640 to the object that you're currently modifying 132 00:06:40.640 --> 00:06:41.593 or acting on. 133 00:06:42.600 --> 00:06:44.200 Okay, and that's how that works. 134 00:06:45.160 --> 00:06:48.210 That's how the methods work and now we are able 135 00:06:48.210 --> 00:06:51.343 to both define the dates now that we're acting on, 136 00:06:52.210 --> 00:06:56.320 and the action we're performing in the same structure. 137 00:06:56.320 --> 00:06:58.430 Now it's a class instead of a dictionary 138 00:06:58.430 --> 00:06:59.910 but essentially it's the same thing, 139 00:06:59.910 --> 00:07:03.560 it allows us to store some data and now it extends 140 00:07:03.560 --> 00:07:05.618 that functionality by allowing us to store some 141 00:07:05.618 --> 00:07:09.073 functions that act on that data as well. 142 00:07:10.120 --> 00:07:12.940 Now of course the average function could receive 143 00:07:12.940 --> 00:07:15.780 some extra arguments, some extra parameters 144 00:07:15.780 --> 00:07:20.400 if you wanted, just as the Dunder init method does. 145 00:07:20.400 --> 00:07:24.640 So you could pass in some name as well if you wanted 146 00:07:24.640 --> 00:07:26.970 and then student_one.average now would just 147 00:07:26.970 --> 00:07:31.410 get something, a friend, some argument there as well. 148 00:07:31.410 --> 00:07:34.430 That would be the second argument, 149 00:07:34.430 --> 00:07:36.610 the second parameter. 150 00:07:36.610 --> 00:07:41.110 Okay so self is populated first by the current object 151 00:07:41.110 --> 00:07:43.670 any other parameters get populated after 152 00:07:43.670 --> 00:07:45.770 from whatever it is inside these brackets. 153 00:07:46.610 --> 00:07:49.970 Anyway, I think I've gone on and on about this enough, 154 00:07:49.970 --> 00:07:53.690 hopefully it's more or less clear that a student 155 00:07:53.690 --> 00:07:56.966 class is just something that allows us to define 156 00:07:56.966 --> 00:08:01.600 what properties are in a student and also 157 00:08:01.600 --> 00:08:05.080 what actions a student can do on it's own data, 158 00:08:05.080 --> 00:08:07.760 or indeed if we so desired on other data 159 00:08:07.760 --> 00:08:09.283 by passing in some arguments. 160 00:08:10.370 --> 00:08:11.973 So that's what a class is, 161 00:08:11.973 --> 00:08:13.000 and that's what an object is. 162 00:08:13.000 --> 00:08:14.830 A way to store some data and 163 00:08:14.830 --> 00:08:16.890 store some actions that relate to it. 164 00:08:16.890 --> 00:08:20.180 That allows us to encapsulate and hold 165 00:08:20.180 --> 00:08:23.680 all relevant functionality in the same structure, 166 00:08:23.680 --> 00:08:26.010 as opposed to having it split over two separate 167 00:08:26.010 --> 00:08:27.560 structures like we did up here. 168 00:08:28.790 --> 00:08:29.730 So that's it for this video, 169 00:08:29.730 --> 00:08:30.830 hopefully that's clear. 170 00:08:30.830 --> 00:08:32.450 And over the coming sections we're gonna 171 00:08:32.450 --> 00:08:34.340 be using this more and more and as you 172 00:08:34.340 --> 00:08:36.480 programme more and more it's gonna sort of be 173 00:08:36.480 --> 00:08:38.570 second nature you're gonna start understanding this 174 00:08:38.570 --> 00:08:41.970 a bit more thoroughly and I'm sure it will get 175 00:08:41.970 --> 00:08:44.310 better and better, if you are confused 176 00:08:44.310 --> 00:08:45.630 please ask a question in the course Q&A 177 00:08:45.630 --> 00:08:48.490 and we'll be happy to help you but it normally 178 00:08:48.490 --> 00:08:50.510 takes a few months to fully click 179 00:08:51.448 --> 00:08:54.220 so of course I'm gonna try to get that to click 180 00:08:54.220 --> 00:08:57.561 earlier by providing with good explanations 181 00:08:57.561 --> 00:09:02.120 but if you are just you know mildly uncomfortable 182 00:09:02.120 --> 00:09:04.370 with all of this, that's fine. 183 00:09:04.370 --> 00:09:06.840 Just keep going, keep programming, 184 00:09:06.840 --> 00:09:09.780 keep writing code and it's gonna all settle down 185 00:09:09.780 --> 00:09:13.340 in your subconscious as you move along. 186 00:09:13.340 --> 00:09:16.183 So that's it for this video, I'll see you on the next one.