WEBVTT 1 00:00:00.130 --> 00:00:01.430 Hi and welcome back. 2 00:00:01.430 --> 00:00:04.273 In this video we're talking about special methods. 3 00:00:05.140 --> 00:00:09.370 In a class, as we've seen, not all methods are the same. 4 00:00:09.370 --> 00:00:10.990 Python sometimes makes a distinction 5 00:00:10.990 --> 00:00:13.390 depending on the method name. 6 00:00:13.390 --> 00:00:16.393 Here is an example of one of those methods. 7 00:00:17.410 --> 00:00:20.700 That's def _init method. 8 00:00:20.700 --> 00:00:23.440 The _init method gets called for you 9 00:00:23.440 --> 00:00:25.410 when you create a new object. 10 00:00:25.410 --> 00:00:28.770 The new blank object is passed to self, 11 00:00:28.770 --> 00:00:30.690 and the name is whatever you put inside 12 00:00:30.690 --> 00:00:32.923 your constructor's arguments. 13 00:00:34.060 --> 00:00:37.070 And then we've sort of defined a new property inside self 14 00:00:37.070 --> 00:00:39.473 when we created the value of our parameter. 15 00:00:40.790 --> 00:00:45.000 So the init, the _init method is a special method 16 00:00:45.000 --> 00:00:48.023 because Python calls it for you automatically. 17 00:00:48.880 --> 00:00:49.930 When you create an object, 18 00:00:49.930 --> 00:00:52.470 you don't have to call it yourself. 19 00:00:52.470 --> 00:00:56.030 So here's a couple other interesting special methods. 20 00:00:56.030 --> 00:00:59.613 First of all let's define a list of movies. 21 00:01:04.670 --> 00:01:06.420 What do you think is going to happen 22 00:01:06.420 --> 00:01:09.993 when I print out the movies class? 23 00:01:10.990 --> 00:01:13.890 Now we looked at printing the students class earlier 24 00:01:13.890 --> 00:01:17.140 and you got some sort of student math thing printed out. 25 00:01:17.140 --> 00:01:19.543 What is the movies class? 26 00:01:21.550 --> 00:01:22.463 Is it an error? 27 00:01:25.760 --> 00:01:28.190 Well, as you can see we get a class list. 28 00:01:28.190 --> 00:01:30.670 So that's some pretty bizarre functionality there. 29 00:01:30.670 --> 00:01:35.023 Does this mean that a list is somehow an object? 30 00:01:36.430 --> 00:01:37.930 Yes. 31 00:01:37.930 --> 00:01:41.370 Pretty much everything in Python is an object 32 00:01:41.370 --> 00:01:43.610 and this list is no different. 33 00:01:43.610 --> 00:01:45.970 Only we've not had to create it 34 00:01:45.970 --> 00:01:48.623 using this constructor like Student type thing, 35 00:01:49.690 --> 00:01:51.730 instead Python's created it for us automatically 36 00:01:51.730 --> 00:01:53.883 when we used this syntax. 37 00:01:54.870 --> 00:01:58.293 You can however print for example hi .__class, 38 00:02:00.500 --> 00:02:03.130 and that's gonna give you a string class. 39 00:02:03.130 --> 00:02:04.223 See, str. 40 00:02:06.030 --> 00:02:09.403 So everything is an object in Python. 41 00:02:10.580 --> 00:02:14.610 What does it mean for a list to be an object? 42 00:02:14.610 --> 00:02:18.390 Well, it means that everything we can do with a list, 43 00:02:18.390 --> 00:02:20.950 we can do with our own classes. 44 00:02:20.950 --> 00:02:22.780 So here's an example. 45 00:02:22.780 --> 00:02:25.593 We can print out the length of movies. 46 00:02:27.100 --> 00:02:30.730 Okay now we know that movies is an object, 47 00:02:30.730 --> 00:02:32.510 so what is going on behind the scene 48 00:02:32.510 --> 00:02:36.620 that is allowing us to call this len function, 49 00:02:36.620 --> 00:02:38.860 give it the movies object, 50 00:02:38.860 --> 00:02:40.113 and it's given us two? 51 00:02:41.820 --> 00:02:43.660 Well, let's do it. 52 00:02:43.660 --> 00:02:45.060 Let's create a class Garage. 53 00:02:46.720 --> 00:02:49.200 And in it we're going to define an init method 54 00:02:50.610 --> 00:02:54.473 that is going to define self.cars to be an empty list. 55 00:02:57.180 --> 00:02:59.777 Now we can create our Garage, 56 00:02:59.777 --> 00:03:03.116 let's say ford is gonna be a Garage. 57 00:03:03.116 --> 00:03:05.640 And now this ford has a, 58 00:03:05.640 --> 00:03:09.920 ford.cars has this cars property here 59 00:03:10.960 --> 00:03:12.840 that is gonna print out an empty list when we print it out 60 00:03:12.840 --> 00:03:15.223 because that's what we've assigned it to. 61 00:03:16.430 --> 00:03:21.430 However we can add, we can say ford.cars.append Fiesta, 62 00:03:22.380 --> 00:03:27.380 and ford.cars.append Focus for example. 63 00:03:27.500 --> 00:03:29.030 Then when we run this programme again, 64 00:03:29.030 --> 00:03:32.533 now we get a list of two items, Fiesta and Focus. 65 00:03:34.670 --> 00:03:39.670 Now what if I wanted to do len of ford? 66 00:03:40.440 --> 00:03:41.500 ford is the Garage, 67 00:03:41.500 --> 00:03:43.820 remember I wanna get the length of ford 68 00:03:43.820 --> 00:03:44.970 because that's gonna tell me 69 00:03:44.970 --> 00:03:47.563 how many cars there are inside my Garage. 70 00:03:48.890 --> 00:03:51.380 Of course when we run this, 71 00:03:51.380 --> 00:03:53.570 we get a TypeError. 72 00:03:53.570 --> 00:03:56.490 Object of type Garage has no len. 73 00:03:56.490 --> 00:03:58.140 Okay that's an interesting error. 74 00:03:59.540 --> 00:04:02.060 It tells us that Python cannot calculate 75 00:04:02.060 --> 00:04:03.095 the length of this Garage 76 00:04:03.095 --> 00:04:03.980 because it doesn't know 77 00:04:03.980 --> 00:04:05.880 what the length of the Garage is meant to be 78 00:04:05.880 --> 00:04:08.073 or it's meant to mean even. 79 00:04:09.530 --> 00:04:11.970 So in order to tell Python 80 00:04:11.970 --> 00:04:14.700 that this Garage has a length, 81 00:04:14.700 --> 00:04:18.150 we have to define another dunder method. 82 00:04:18.150 --> 00:04:19.550 And that's __len__. 83 00:04:21.970 --> 00:04:25.350 Again it takes in self like all methods do, 84 00:04:25.350 --> 00:04:28.070 and then it has to return the length of the Garage, 85 00:04:28.070 --> 00:04:30.970 in this case how many cars there are. 86 00:04:30.970 --> 00:04:33.563 So it's gonna return the length of self.cars. 87 00:04:34.860 --> 00:04:36.810 Now when we run this programme again, 88 00:04:36.810 --> 00:04:38.570 we see that two gets printed out 89 00:04:38.570 --> 00:04:40.990 even though we've called len of ford, 90 00:04:40.990 --> 00:04:42.210 our Garage object, 91 00:04:42.210 --> 00:04:45.283 instead of len of ford.cars. 92 00:04:46.650 --> 00:04:49.170 That's because when we call len, 93 00:04:49.170 --> 00:04:52.000 ford comes over to here, self, 94 00:04:52.000 --> 00:04:54.793 and then we return the length of self.cars. 95 00:04:56.170 --> 00:05:00.420 The list also has this __len method 96 00:05:00.420 --> 00:05:04.727 that gives us the number of items in the list. 97 00:05:04.727 --> 00:05:09.270 That's a slightly more complicated definition, 98 00:05:09.270 --> 00:05:11.170 but nonetheless what we have to know 99 00:05:11.170 --> 00:05:15.500 is that this len__ method allows us to do something 100 00:05:15.500 --> 00:05:18.100 and return a value that we get returned to us 101 00:05:18.100 --> 00:05:21.763 when we call the len function on our object. 102 00:05:22.720 --> 00:05:27.720 Let's do something like for example ford zero. 103 00:05:29.730 --> 00:05:32.670 ford zero should give us the first car 104 00:05:32.670 --> 00:05:35.460 in the queue of cars that we're working with for example, 105 00:05:35.460 --> 00:05:36.940 in this case it should be Fiesta 106 00:05:36.940 --> 00:05:39.330 because that's the first car that we appended. 107 00:05:39.330 --> 00:05:40.450 Of course if we run this, 108 00:05:40.450 --> 00:05:45.030 we'll get that Garage object does not support indexing. 109 00:05:45.030 --> 00:05:47.610 Remember this is accessing something by an index. 110 00:05:47.610 --> 00:05:49.053 Python calls it indexing. 111 00:05:50.150 --> 00:05:51.330 What to do? 112 00:05:51.330 --> 00:05:52.163 Well, you guessed it. 113 00:05:52.163 --> 00:05:54.433 We'll define another dunder method. 114 00:05:55.330 --> 00:05:57.260 Here's a challenge for you. 115 00:05:57.260 --> 00:05:59.450 Pause the video and go and find out 116 00:05:59.450 --> 00:06:03.690 how you can index your own custom object. 117 00:06:03.690 --> 00:06:05.880 It's gonna be a bit of a challenge, a bit tricky, 118 00:06:05.880 --> 00:06:07.410 but I'm sure you can do it. 119 00:06:07.410 --> 00:06:09.410 Okay and I'm sure you got that. 120 00:06:09.410 --> 00:06:11.693 We have to define a new dunder method, 121 00:06:12.830 --> 00:06:14.530 and this time it's called getitem, 122 00:06:16.170 --> 00:06:17.890 and it takes self of course, 123 00:06:17.890 --> 00:06:20.220 the object that we're calling this on, 124 00:06:20.220 --> 00:06:25.220 and it also takes the index that we're trying to access, i. 125 00:06:25.277 --> 00:06:28.530 Remember this is just another way of calling 126 00:06:28.530 --> 00:06:32.870 ford.__getitem with ford, 127 00:06:32.870 --> 00:06:37.373 sorry, Garage, Garage.__getitem with ford and zero. 128 00:06:38.630 --> 00:06:42.503 Okay this is syntactic sugar for this. 129 00:06:43.487 --> 00:06:45.930 What we're doing is calling the getitem function 130 00:06:45.930 --> 00:06:47.550 of the Garage class, 131 00:06:47.550 --> 00:06:48.383 and we're passing to it 132 00:06:48.383 --> 00:06:50.240 the ford object that we've created 133 00:06:50.240 --> 00:06:53.040 and the index that we wanna access which is zero. 134 00:06:53.040 --> 00:06:58.040 And all we have to do is return self.cars dot i, 135 00:06:58.290 --> 00:06:59.123 so the cars' i. 136 00:07:00.389 --> 00:07:01.730 And that's gonna give us 137 00:07:01.730 --> 00:07:05.610 the i'th index of the self.cars list. 138 00:07:05.610 --> 00:07:06.443 Let's run it. 139 00:07:07.492 --> 00:07:08.790 And there you have it. 140 00:07:08.790 --> 00:07:10.433 We get Fiesta back. 141 00:07:11.280 --> 00:07:14.560 So these are two pretty important dunder methods 142 00:07:14.560 --> 00:07:18.120 that can be quite useful in many circumstances. 143 00:07:18.120 --> 00:07:22.420 Whenever you want an object that will store things 144 00:07:22.420 --> 00:07:24.630 and that you can access, 145 00:07:24.630 --> 00:07:27.200 these two methods can be pretty useful. 146 00:07:27.200 --> 00:07:29.130 Now as soon as you've implemented 147 00:07:29.130 --> 00:07:32.010 the len and getitem methods, 148 00:07:32.010 --> 00:07:34.640 some new functionality gets unlocked, 149 00:07:34.640 --> 00:07:38.673 and that is that you can iterate over your Garage, 150 00:07:40.180 --> 00:07:42.570 you can use a for loop on your Garage, 151 00:07:42.570 --> 00:07:45.770 but only when you have these two methods defined, 152 00:07:45.770 --> 00:07:47.840 okay because Python is gonna use these two methods 153 00:07:47.840 --> 00:07:49.500 in the background of the for loop 154 00:07:49.500 --> 00:07:51.333 in order to iterate over it. 155 00:07:52.720 --> 00:07:53.553 And there you have it, 156 00:07:53.553 --> 00:07:55.453 you get Fiesta and Focus. 157 00:07:57.380 --> 00:07:59.080 So that's some pretty cool stuff. 158 00:07:59.080 --> 00:08:00.810 The first time I learned about this, 159 00:08:00.810 --> 00:08:05.000 I was, pardon the expression, flabbergasted, 160 00:08:05.000 --> 00:08:07.360 because I really didn't know 161 00:08:07.360 --> 00:08:10.288 much about these dunder methods, 162 00:08:10.288 --> 00:08:11.370 and it's pretty interesting 163 00:08:11.370 --> 00:08:13.420 because everything you can do in Python, 164 00:08:13.420 --> 00:08:16.350 all the built-in functions like len and sum 165 00:08:17.240 --> 00:08:19.760 and all the constructs like for 166 00:08:19.760 --> 00:08:24.725 are all built on objects having these special methods. 167 00:08:24.725 --> 00:08:27.730 And so learning about these is really exciting. 168 00:08:27.730 --> 00:08:31.470 And we're gonna learn about them as we go along the course. 169 00:08:31.470 --> 00:08:33.430 But I wanted to give you 170 00:08:36.840 --> 00:08:39.910 another two special methods in fact 171 00:08:39.910 --> 00:08:43.823 that are the dunder repr and dunder string. 172 00:08:44.690 --> 00:08:47.160 So let's implement __repr. 173 00:08:47.160 --> 00:08:49.740 This is how you define the repr. 174 00:08:49.740 --> 00:08:52.610 And this repr should be used to print out 175 00:08:52.610 --> 00:08:55.233 a string representing the object. 176 00:08:56.820 --> 00:09:00.070 So let's once again put self in here. 177 00:09:00.070 --> 00:09:01.857 And we can return, 178 00:09:01.857 --> 00:09:03.120 sorry, this should be, 179 00:09:03.120 --> 00:09:04.810 I said this should be used to print out a string, 180 00:09:04.810 --> 00:09:07.160 that's a lie, it should be used to return a string 181 00:09:07.160 --> 00:09:08.623 that represents the object. 182 00:09:09.860 --> 00:09:14.860 So we can define this as being a Garage, 183 00:09:15.070 --> 00:09:17.220 and inside of it we're gonna put self.cars. 184 00:09:18.490 --> 00:09:20.970 That really represents our Garage. 185 00:09:20.970 --> 00:09:22.120 It's a Garage object 186 00:09:22.120 --> 00:09:24.513 and it has this list of cars inside of it. 187 00:09:27.224 --> 00:09:31.163 The dunder string, str, 188 00:09:32.090 --> 00:09:33.640 should be used to return 189 00:09:34.720 --> 00:09:37.440 a string that tells the user 190 00:09:37.440 --> 00:09:39.110 some information about this Garage 191 00:09:39.110 --> 00:09:42.900 such that if you were to print the object out, 192 00:09:42.900 --> 00:09:46.160 the user might gain some useful insights 193 00:09:46.160 --> 00:09:48.283 about the Garage you're printing out. 194 00:09:49.850 --> 00:09:52.350 So in this case we should return something like f, 195 00:09:53.410 --> 00:09:57.780 and Garage with len of self cars. 196 00:10:00.620 --> 00:10:03.360 Notice how the repr, the __repr, 197 00:10:03.360 --> 00:10:07.640 returns some sort of codified version of this Garage. 198 00:10:07.640 --> 00:10:09.040 It includes everything we need 199 00:10:09.040 --> 00:10:11.430 in order to represent this object. 200 00:10:11.430 --> 00:10:13.170 And the str method 201 00:10:14.350 --> 00:10:16.690 returned something that a user might want to read. 202 00:10:16.690 --> 00:10:19.340 That's really the difference between these two, okay. 203 00:10:20.350 --> 00:10:22.550 Notice how here we've accessed len of self. 204 00:10:22.550 --> 00:10:23.620 I love this. 205 00:10:23.620 --> 00:10:24.980 self is the object, 206 00:10:24.980 --> 00:10:26.270 so when you call len of self, 207 00:10:26.270 --> 00:10:28.480 you're really calling the len method 208 00:10:28.480 --> 00:10:29.730 that you defined up here. 209 00:10:30.970 --> 00:10:34.323 Now when you print ford down here, 210 00:10:36.300 --> 00:10:39.050 you'll see the str gets used. 211 00:10:39.050 --> 00:10:40.293 Garage with two cars. 212 00:10:41.900 --> 00:10:45.313 So when does the __repr method get used? 213 00:10:46.740 --> 00:10:49.210 The __repr method gets used 214 00:10:49.210 --> 00:10:53.080 particularly when you are coding, 215 00:10:53.080 --> 00:10:54.520 when you're using a debugger, 216 00:10:54.520 --> 00:10:57.980 we're gonna learn what that is in a few sections' time. 217 00:10:57.980 --> 00:11:01.510 It can be really useful for you to look at the object 218 00:11:01.510 --> 00:11:04.920 and understand exactly what it is made up of, 219 00:11:04.920 --> 00:11:06.640 in this case cars. 220 00:11:06.640 --> 00:11:07.900 As we look into debugging, 221 00:11:07.900 --> 00:11:10.561 we're gonna look more at the repr function 222 00:11:10.561 --> 00:11:12.070 and why it can be useful. 223 00:11:12.070 --> 00:11:14.030 So don't worry too much about it for now. 224 00:11:14.030 --> 00:11:16.100 Just know that the str function can be really useful 225 00:11:16.100 --> 00:11:18.033 when printing things out to a user. 226 00:11:18.950 --> 00:11:21.940 However if you are only going to implement 227 00:11:21.940 --> 00:11:25.043 one of these two, choose the repr. 228 00:11:26.370 --> 00:11:29.180 You can just delete the repr, 229 00:11:29.180 --> 00:11:32.453 yeah, you can turn the str into a repr, 230 00:11:34.067 --> 00:11:35.563 delete this repr here, 231 00:11:37.020 --> 00:11:39.610 and make it print something a user will wanna see, 232 00:11:39.610 --> 00:11:41.130 and this is a better approach. 233 00:11:41.130 --> 00:11:42.663 When you now print ford, 234 00:11:42.663 --> 00:11:44.700 it's gonna call the repr function 235 00:11:44.700 --> 00:11:46.560 instead of the str function, 236 00:11:46.560 --> 00:11:48.740 because repr is sort of a bit more important. 237 00:11:48.740 --> 00:11:50.840 So if you're only gonna implement one, 238 00:11:50.840 --> 00:11:51.960 implement the repr, 239 00:11:51.960 --> 00:11:55.330 but of course if you can implement both, 240 00:11:55.330 --> 00:11:59.140 one, for a more code-oriented description, 241 00:11:59.140 --> 00:12:01.850 and the other one for a more user-oriented description, 242 00:12:01.850 --> 00:12:03.083 this is much better. 243 00:12:03.970 --> 00:12:07.760 I am a strong proponent that every class you create 244 00:12:07.760 --> 00:12:11.593 should have a repr and an str function, methods, sorry. 245 00:12:12.630 --> 00:12:15.277 Okay, there's many more magic dunder methods 246 00:12:15.277 --> 00:12:20.277 you can implement including some to add objects together, 247 00:12:20.330 --> 00:12:22.960 after all integers are also objects. 248 00:12:22.960 --> 00:12:25.730 You can overload what Boolean operators do, 249 00:12:25.730 --> 00:12:28.010 you can make your objects callable, 250 00:12:28.010 --> 00:12:30.230 and you can do things like add context managers, 251 00:12:30.230 --> 00:12:31.640 we're gonna look at what those are, 252 00:12:31.640 --> 00:12:32.630 and much much more. 253 00:12:32.630 --> 00:12:34.240 We'll be learning about a lot of these 254 00:12:34.240 --> 00:12:35.760 dunder methods throughout the course 255 00:12:35.760 --> 00:12:38.150 because this again is the most comprehensive 256 00:12:38.150 --> 00:12:40.280 Python course that you can take 257 00:12:40.280 --> 00:12:43.173 so I wanna cover everything about Python. 258 00:12:44.110 --> 00:12:44.980 That's it for this video. 259 00:12:44.980 --> 00:12:46.430 I know it's getting a bit more advanced, 260 00:12:46.430 --> 00:12:47.950 so are you still with me? 261 00:12:47.950 --> 00:12:50.020 When we get to building our next project, 262 00:12:50.020 --> 00:12:52.780 everything is gonna be a bit clearer. 263 00:12:52.780 --> 00:12:53.990 So that's it for this video. 264 00:12:53.990 --> 00:12:55.440 I'll see you on the next one.