WEBVTT 1 00:00:00.210 --> 00:00:01.650 Hi, welcome back. 2 00:00:01.650 --> 00:00:03.510 In this video we're looking at 3 00:00:03.510 --> 00:00:06.723 class method and static method decorators. 4 00:00:07.780 --> 00:00:10.010 We've looked at how we can define classes and methods 5 00:00:10.010 --> 00:00:11.550 and including some special methods, 6 00:00:11.550 --> 00:00:14.660 like dunder init and dunder len. 7 00:00:14.660 --> 00:00:16.770 And all these methods had something in common, 8 00:00:16.770 --> 00:00:19.780 which is the self parameter at the start. 9 00:00:19.780 --> 00:00:21.860 As a reminder, I've written here some code 10 00:00:21.860 --> 00:00:23.940 which is a stringed class, that we've already seen. 11 00:00:23.940 --> 00:00:26.450 We've got self, and self. 12 00:00:26.450 --> 00:00:29.310 Remember, we can call this parameter whatever we want, 13 00:00:29.310 --> 00:00:32.460 but the convention in Python is to call it self. 14 00:00:32.460 --> 00:00:34.490 The most important thing about this parameter 15 00:00:34.490 --> 00:00:36.520 is the value that it points to, 16 00:00:36.520 --> 00:00:37.973 the value that it represents. 17 00:00:38.920 --> 00:00:40.263 Which is the object. 18 00:00:41.350 --> 00:00:44.710 When we create a new object from the Student class 19 00:00:44.710 --> 00:00:46.750 and we call any of its methods 20 00:00:46.750 --> 00:00:49.520 we are automatically passing in the self parameter, 21 00:00:49.520 --> 00:00:50.353 and we've seen that. 22 00:00:50.353 --> 00:00:52.340 Let me just put some code in here, 23 00:00:52.340 --> 00:00:55.070 which creates a new student, Rolf, 24 00:00:55.070 --> 00:00:57.240 and it appends a couple of marks to him 25 00:00:57.240 --> 00:00:59.843 and then prints out the average function. 26 00:01:00.950 --> 00:01:03.170 Remember what this is doing is just 27 00:01:03.170 --> 00:01:07.380 doing the student.average and passing in rolf. 28 00:01:07.380 --> 00:01:11.270 So we're accessing the average method of the student class 29 00:01:11.270 --> 00:01:13.833 and we're passing rolf in a the first argument. 30 00:01:14.670 --> 00:01:16.290 Rolf.average does the same thing, 31 00:01:16.290 --> 00:01:19.023 only Python does this for us in the background. 32 00:01:22.550 --> 00:01:27.550 So when we do object.method that really is doing 33 00:01:27.660 --> 00:01:30.813 class.method and then passing the object in there. 34 00:01:31.930 --> 00:01:36.380 And this knowledge lets us do some pretty weird stuff. 35 00:01:36.380 --> 00:01:38.680 I don't recommend you do this as it's gonna break things 36 00:01:38.680 --> 00:01:42.657 but we could do something like student.average, hello, 37 00:01:44.760 --> 00:01:45.993 passing in a string. 38 00:01:47.160 --> 00:01:48.810 Obviously this is not gonna work 39 00:01:48.810 --> 00:01:52.710 because the average method here expects an object 40 00:01:52.710 --> 00:01:55.080 that has a marks property. 41 00:01:55.080 --> 00:01:58.170 A string does not have a marks property 42 00:01:58.170 --> 00:01:59.700 so when we run this it's gonna tell us 43 00:01:59.700 --> 00:02:02.143 that a string doesn't have an attribute, marks. 44 00:02:03.120 --> 00:02:04.530 See? 45 00:02:04.530 --> 00:02:06.670 But, nonetheless you can do this because 46 00:02:06.670 --> 00:02:08.620 self can be anything you want. 47 00:02:08.620 --> 00:02:10.730 As I said earlier, the important thing 48 00:02:10.730 --> 00:02:12.980 is the value that self represents, 49 00:02:12.980 --> 00:02:16.160 which should be an object of type student 50 00:02:16.160 --> 00:02:18.710 so that it has these marks, 51 00:02:18.710 --> 00:02:20.813 sorry, name, school, and marks properties. 52 00:02:22.580 --> 00:02:25.430 Just remember that self is a parameter like any other 53 00:02:25.430 --> 00:02:27.560 and you can give it any value you want. 54 00:02:27.560 --> 00:02:31.080 However, doing hello.marks is not gonna work 55 00:02:31.080 --> 00:02:33.293 because the string doesn't have that property. 56 00:02:34.400 --> 00:02:36.763 Anyway, so why is this important? 57 00:02:37.900 --> 00:02:40.190 This first type of method we've looked at 58 00:02:40.190 --> 00:02:42.830 is commonly called an instance method. 59 00:02:42.830 --> 00:02:46.278 It's one that takes the core object as the first argument, 60 00:02:46.278 --> 00:02:47.880 and that's self. 61 00:02:47.880 --> 00:02:49.730 So the caller here is rolf. 62 00:02:49.730 --> 00:02:51.860 When we call the average, it takes in rolf 63 00:02:51.860 --> 00:02:53.943 as the first argument. 64 00:02:54.810 --> 00:02:57.650 This is called an instance method. 65 00:02:57.650 --> 00:03:00.230 There are two more types of method. 66 00:03:00.230 --> 00:03:05.230 One that takes the caller's class as the first argument, 67 00:03:05.550 --> 00:03:08.000 and one that takes nothing as the first argument. 68 00:03:08.850 --> 00:03:12.500 So instance method takes the object, that's rolf, 69 00:03:12.500 --> 00:03:13.750 as the first argument, 70 00:03:13.750 --> 00:03:15.000 then we've got another type, 71 00:03:15.000 --> 00:03:19.140 which takes the class, that's student, as the first argument 72 00:03:19.140 --> 00:03:22.193 and there's one that takes nothing as the first argument. 73 00:03:23.560 --> 00:03:26.410 Let's have a look at one that take the caller's class 74 00:03:26.410 --> 00:03:27.773 as the first argument. 75 00:03:28.740 --> 00:03:31.440 We're going to define a new class here, 76 00:03:31.440 --> 00:03:33.250 and I'm just going to show you the syntax, 77 00:03:33.250 --> 00:03:35.590 and in the next video we're going to look at some examples. 78 00:03:35.590 --> 00:03:37.760 So these examples are going to be pretty terrible. 79 00:03:37.760 --> 00:03:40.370 We're gonna define a class which we're gonna call foo 80 00:03:41.370 --> 00:03:46.173 and in it we're gonna have an @classmethod that is hi. 81 00:03:49.420 --> 00:03:51.970 So notice, by the way, I've not really said this before, 82 00:03:51.970 --> 00:03:56.010 but you do not need an init method on every class. 83 00:03:56.010 --> 00:03:58.600 You can just ignore it and then you just won't 84 00:03:58.600 --> 00:04:00.190 define any properties to begin with. 85 00:04:00.190 --> 00:04:02.190 That's totally fine, you don't need one. 86 00:04:04.060 --> 00:04:07.300 So we've got this @classmethod decorator 87 00:04:07.300 --> 00:04:08.133 and we know it's a decorator 88 00:04:08.133 --> 00:04:10.830 because it has the @ symbol in front. 89 00:04:10.830 --> 00:04:12.980 And now I've renamed the first argument 90 00:04:12.980 --> 00:04:15.513 to be cls instead of self. 91 00:04:16.520 --> 00:04:19.090 That's just to tell me that this is a class method, 92 00:04:19.090 --> 00:04:21.820 and that this parameter is going to hold 93 00:04:21.820 --> 00:04:24.840 the value of the class that it was called with. 94 00:04:24.840 --> 00:04:26.053 So that's foo. 95 00:04:27.550 --> 00:04:30.513 Let's print cls.__name__, okay? 96 00:04:33.690 --> 00:04:38.690 Then I am going to create a object of type foo 97 00:04:39.560 --> 00:04:42.253 and I'm going to say my object.hi. 98 00:04:43.690 --> 00:04:46.840 Now, important, what's happening in the background 99 00:04:46.840 --> 00:04:50.440 is that not my object, my emphasis not, getting passed 100 00:04:50.440 --> 00:04:51.960 as the first argument. 101 00:04:51.960 --> 00:04:54.420 Now the object's class is getting passed 102 00:04:54.420 --> 00:04:55.530 as the first argument. 103 00:04:55.530 --> 00:04:59.051 So what do you think we're going to see printed out here? 104 00:04:59.051 --> 00:05:00.070 I hope you got that right. 105 00:05:00.070 --> 00:05:03.423 What we'll see is foo, so let's run that. 106 00:05:05.350 --> 00:05:09.090 And as you can see what we see printed out here is foo. 107 00:05:09.090 --> 00:05:14.090 That's because cls.__name__ gives us the name of the class 108 00:05:14.470 --> 00:05:17.473 and cls is the class, so that's foo. 109 00:05:18.760 --> 00:05:20.670 Now, terrible example, I just wanted 110 00:05:20.670 --> 00:05:22.180 to show you the syntax, 111 00:05:22.180 --> 00:05:24.920 and again cls is just a name for the parameter, 112 00:05:24.920 --> 00:05:26.170 you can call it whatever you want. 113 00:05:26.170 --> 00:05:29.400 If you wanted to be really quite silly, 114 00:05:29.400 --> 00:05:30.850 you could call it self. 115 00:05:30.850 --> 00:05:32.460 Terrible idea, don't do that. 116 00:05:32.460 --> 00:05:33.790 Because then it's just gonna confuse you. 117 00:05:33.790 --> 00:05:36.160 Okay, this is not self, this is not the object 118 00:05:36.160 --> 00:05:37.700 that you're currently dealing with. 119 00:05:37.700 --> 00:05:40.850 cls is the convention because it stands for class. 120 00:05:40.850 --> 00:05:43.950 Just call it that, I think that's a better idea. 121 00:05:43.950 --> 00:05:48.950 Now another example, we could, instead of @classmethod, 122 00:05:49.190 --> 00:05:53.230 define class Bar, that's gonna have @staticmethod. 123 00:05:59.550 --> 00:06:02.810 This static method takes no parameters, 124 00:06:02.810 --> 00:06:05.360 notice that there's nothing inside there 125 00:06:05.360 --> 00:06:06.280 and it just prints something, 126 00:06:06.280 --> 00:06:07.640 hello, I don't take parameters. 127 00:06:07.640 --> 00:06:09.413 I don't take parameters. 128 00:06:11.060 --> 00:06:12.810 I just have this really bad habit of 129 00:06:12.810 --> 00:06:15.180 calling parameters arguments and vice versa. 130 00:06:15.180 --> 00:06:16.510 Don't fall into that bad habit. 131 00:06:16.510 --> 00:06:18.950 Just call them parameters when they're here, 132 00:06:18.950 --> 00:06:21.173 arguments when they are here. 133 00:06:22.270 --> 00:06:23.853 And that's the correct naming. 134 00:06:24.690 --> 00:06:28.143 Now we can say another_object is Bar, 135 00:06:29.470 --> 00:06:32.403 and we can say another_object.hi, 136 00:06:33.290 --> 00:06:34.940 and we can run this. 137 00:06:34.940 --> 00:06:36.010 And now you see that it says 138 00:06:36.010 --> 00:06:37.793 Hello, I don't take parameters. 139 00:06:39.150 --> 00:06:40.133 So that's good. 140 00:06:41.870 --> 00:06:45.240 These pieces of syntax are just so that 141 00:06:45.240 --> 00:06:48.283 you can modify what the method takes in. 142 00:06:49.660 --> 00:06:52.693 How you use them is totally up to you. 143 00:06:53.640 --> 00:06:57.500 So, frequently the class methods are going to be used 144 00:06:57.500 --> 00:06:58.930 when you want something that 145 00:06:58.930 --> 00:07:01.590 wants to have access to the class. 146 00:07:01.590 --> 00:07:03.200 And static methods are going to be used 147 00:07:03.200 --> 00:07:05.090 when you want a method here that 148 00:07:05.090 --> 00:07:07.190 doesn't use the current object or the class 149 00:07:07.190 --> 00:07:10.070 but it's somehow related to the class. 150 00:07:10.070 --> 00:07:12.750 In the next video we're going to look at a couple examples. 151 00:07:12.750 --> 00:07:15.520 So just remember this syntax for now. 152 00:07:15.520 --> 00:07:17.370 And now in the next video we're gonna look at some examples, 153 00:07:17.370 --> 00:07:18.203 and throughout the course 154 00:07:18.203 --> 00:07:20.420 we're gonna be using this quite a few times. 155 00:07:20.420 --> 00:07:22.330 So don't worry if it doesn't fully click. 156 00:07:22.330 --> 00:07:23.730 I just wanted to show you what it looks like. 157 00:07:23.730 --> 00:07:25.510 Now, let's move on to the next video. 158 00:07:25.510 --> 00:07:26.460 I'll see you there.