WEBVTT 1 00:00:00.350 --> 00:00:01.500 Hi, and welcome back. 2 00:00:01.500 --> 00:00:05.217 In this video, we're going to talk about errors in Python. 3 00:00:05.217 --> 00:00:07.610 And we're going to talk about quite a few of them. 4 00:00:07.610 --> 00:00:10.360 I just wanna tell you something about these built in errors, 5 00:00:10.360 --> 00:00:11.950 the errors that come with Python, 6 00:00:11.950 --> 00:00:13.810 so that when they appear in your programmes, 7 00:00:13.810 --> 00:00:16.003 you'll know exactly what they mean, 8 00:00:16.003 --> 00:00:18.780 and you don't have to, waste time trying to find out 9 00:00:18.780 --> 00:00:21.853 what they are and how they happened. 10 00:00:22.740 --> 00:00:25.040 So we're going to talk about a few errors. 11 00:00:25.040 --> 00:00:28.620 We're going to talk about the Index Error and what it means. 12 00:00:28.620 --> 00:00:32.030 A Key Error, and Name Error. 13 00:00:32.030 --> 00:00:34.210 These types of error happened when you access 14 00:00:34.210 --> 00:00:38.280 something that didn't exist in the wrong way. 15 00:00:38.280 --> 00:00:39.800 We're going to look at Attribute Error, 16 00:00:39.800 --> 00:00:41.650 which is fairly related. 17 00:00:41.650 --> 00:00:43.253 And then we look at Not Implemented Error. 18 00:00:43.253 --> 00:00:46.960 This is an error that you can use, that can be really handy. 19 00:00:46.960 --> 00:00:49.930 We're going to look at Runtime Error, quite a generic error. 20 00:00:49.930 --> 00:00:52.120 Then we're going to look at also Syntax Error. 21 00:00:52.120 --> 00:00:55.290 This happens when you mess up, and writing Python code, 22 00:00:55.290 --> 00:00:57.610 you don't write proper Python. 23 00:00:57.610 --> 00:01:00.760 We're going to look at Indentation Error and Tab Error. 24 00:01:00.760 --> 00:01:04.880 And these happen when you mess up 25 00:01:04.880 --> 00:01:06.730 with the spaces or tabs, 26 00:01:06.730 --> 00:01:08.880 or when you mix them together. 27 00:01:08.880 --> 00:01:12.610 We're going to look at Type Error and Value Error. 28 00:01:12.610 --> 00:01:15.070 And these happen when you give the wrong values 29 00:01:15.070 --> 00:01:19.370 or values of the wrong type to some built in functions. 30 00:01:19.370 --> 00:01:21.290 Then we're going to look at Import error, 31 00:01:21.290 --> 00:01:23.120 and when that happens, 32 00:01:23.120 --> 00:01:24.730 and we're also going to briefly talk about 33 00:01:24.730 --> 00:01:27.433 Deprecation Warning, and how you can use that. 34 00:01:28.910 --> 00:01:30.160 So the Index Error, 35 00:01:30.160 --> 00:01:32.970 the Index Error happens when you do something like this, 36 00:01:32.970 --> 00:01:33.920 the first three lines, by the way, 37 00:01:33.920 --> 00:01:36.710 are just the Python introduction type thing, 38 00:01:36.710 --> 00:01:39.300 when you run Python on your console, 39 00:01:39.300 --> 00:01:42.670 I'll teach you how to do that in a couple of sections time. 40 00:01:42.670 --> 00:01:46.010 Basically, we've defined friends as Rolf and Anne. 41 00:01:46.010 --> 00:01:47.940 And then we've accessed to friends two. 42 00:01:47.940 --> 00:01:50.400 But of course, friends 2 doesn't exist 43 00:01:50.400 --> 00:01:51.920 because friends only has two things, 44 00:01:51.920 --> 00:01:53.730 zero and one. 45 00:01:53.730 --> 00:01:56.600 When you access friends 2 that's an Index Error, 46 00:01:56.600 --> 00:01:59.480 because you accessed an index that didn't exist. 47 00:01:59.480 --> 00:02:01.730 In this case it tells you that the list index 48 00:02:01.730 --> 00:02:02.770 was out of range, 49 00:02:02.770 --> 00:02:06.400 you're trying to access two but you've only go up to one. 50 00:02:06.400 --> 00:02:08.720 And this is a type of Index Error. 51 00:02:08.720 --> 00:02:12.220 Notice how the trace back tells you where the file, 52 00:02:12.220 --> 00:02:16.731 the file line and the module or function that you ran it in. 53 00:02:16.731 --> 00:02:19.540 And in this time, in this case, because I'm running Python, 54 00:02:19.540 --> 00:02:20.490 in interpreter mode, 55 00:02:20.490 --> 00:02:23.413 that means you're just running Python interactively, 56 00:02:24.830 --> 00:02:26.740 it executes as soon as you type. 57 00:02:26.740 --> 00:02:30.420 So the file is just the thing I'm typing this with, 58 00:02:30.420 --> 00:02:31.680 this file is called. 59 00:02:31.680 --> 00:02:33.760 And line is number one because again, 60 00:02:33.760 --> 00:02:36.690 every line is number one in interpreter mode, 61 00:02:36.690 --> 00:02:38.740 and the module is just the module I'm running with. 62 00:02:38.740 --> 00:02:41.220 And that just means when it says in module, 63 00:02:41.220 --> 00:02:44.390 that just means you are running the line directly, 64 00:02:44.390 --> 00:02:46.340 you're not going through any functions. 65 00:02:47.790 --> 00:02:50.990 So there's an index error, happens when you access an index 66 00:02:50.990 --> 00:02:53.943 that didn't exist or an index that is otherwise incorrect. 67 00:02:55.490 --> 00:02:57.070 Then we've got Key Error, 68 00:02:57.070 --> 00:03:00.100 and this is fairly similar to the one we saw earlier. 69 00:03:00.100 --> 00:03:04.440 But now we have the proper code, movie name, movie director, 70 00:03:04.440 --> 00:03:06.780 and movie release. 71 00:03:06.780 --> 00:03:08.853 However, it should be year. 72 00:03:09.800 --> 00:03:13.370 And remember, in our milestone one, we used the year key. 73 00:03:13.370 --> 00:03:15.350 And let's assume that we've used the year key 74 00:03:15.350 --> 00:03:17.180 for this bit of code as well. 75 00:03:17.180 --> 00:03:20.700 What we'll get then is this nice trace back. 76 00:03:20.700 --> 00:03:23.200 Again, what we've got now is a Key Error. 77 00:03:23.200 --> 00:03:26.820 And the description of this Key Error is pretty terrible. 78 00:03:26.820 --> 00:03:29.070 I don't like this description because it just says 79 00:03:29.070 --> 00:03:31.240 the key that you misused. 80 00:03:31.240 --> 00:03:33.343 So it says Key Error release. 81 00:03:35.210 --> 00:03:36.780 Not terribly helpful. 82 00:03:36.780 --> 00:03:38.810 But now you know that you've used the release key 83 00:03:38.810 --> 00:03:40.490 at some point where you shouldn't have. 84 00:03:40.490 --> 00:03:42.570 And then if the trace back is good, 85 00:03:42.570 --> 00:03:45.380 it'll give you which line that ever happened. 86 00:03:45.380 --> 00:03:47.940 You've used it here in movie release. 87 00:03:47.940 --> 00:03:49.810 And then again, it says, 88 00:03:49.810 --> 00:03:52.840 and this line was inside show movie details. 89 00:03:52.840 --> 00:03:54.680 Show movie details was executed, 90 00:03:54.680 --> 00:03:57.090 as show movie details with movie inside it 91 00:03:57.090 --> 00:03:59.490 and that happened inside show movies. 92 00:03:59.490 --> 00:04:01.640 That was executed inside the menu, 93 00:04:01.640 --> 00:04:03.900 and the menu was executed inside the module 94 00:04:03.900 --> 00:04:06.320 or the file that you executed. 95 00:04:06.320 --> 00:04:07.580 Notice how all of these things 96 00:04:07.580 --> 00:04:10.390 are inside a file called app dot py. 97 00:04:10.390 --> 00:04:13.543 Because that's what our file was called in milestone one. 98 00:04:14.730 --> 00:04:17.960 So the Key Error happens when you use a key incorrectly, 99 00:04:17.960 --> 00:04:19.870 or otherwise the key doesn't exist. 100 00:04:19.870 --> 00:04:22.003 But movie is a dictionary. 101 00:04:23.357 --> 00:04:25.910 Okay, so we've got movie being a dictionary, 102 00:04:25.910 --> 00:04:29.300 and then we access to the incorrect key. 103 00:04:29.300 --> 00:04:31.570 If movie was a list, remember from the last video, 104 00:04:31.570 --> 00:04:33.463 we would get a different type of error. 105 00:04:34.690 --> 00:04:37.150 We also have Name Error. 106 00:04:37.150 --> 00:04:39.900 And the Name Error happens as we saw earlier 107 00:04:39.900 --> 00:04:41.040 in the last video, 108 00:04:41.040 --> 00:04:43.770 when the variable is not defined. 109 00:04:43.770 --> 00:04:46.640 So here I've just launched Python and I said print hello, 110 00:04:46.640 --> 00:04:47.860 the variable hello. 111 00:04:47.860 --> 00:04:49.800 But of course, hello is not defined. 112 00:04:49.800 --> 00:04:54.053 So I get a Name Error that says name hello is not defined. 113 00:04:54.950 --> 00:04:58.310 Of course print of the string hello would still work, 114 00:04:58.310 --> 00:05:00.850 because the string hello is just a set of characters 115 00:05:00.850 --> 00:05:03.070 and numbers or symbols. 116 00:05:03.070 --> 00:05:05.820 And Python doesn't evaluate it to a variable at all. 117 00:05:05.820 --> 00:05:07.200 So that would work. 118 00:05:07.200 --> 00:05:09.550 But printing the variable hello doesn't work. 119 00:05:09.550 --> 00:05:12.560 This is actually a really common error for my students 120 00:05:12.560 --> 00:05:15.440 where they forget to or you guys forget to 121 00:05:15.440 --> 00:05:17.860 put the quotation marks around the string, 122 00:05:17.860 --> 00:05:19.140 happens to everybody. 123 00:05:19.140 --> 00:05:19.973 And just remember, 124 00:05:19.973 --> 00:05:22.630 if you get a Name Error for something that it is in a print, 125 00:05:22.630 --> 00:05:25.540 just check whether it needs quotation marks around it. 126 00:05:25.540 --> 00:05:26.930 That's that's a fairly popular thing. 127 00:05:26.930 --> 00:05:28.280 So just keep your eyes out. 128 00:05:29.610 --> 00:05:31.370 Let's also look at Attribute Error. 129 00:05:31.370 --> 00:05:33.000 Attribute error is pretty popular 130 00:05:33.000 --> 00:05:35.100 when you're starting to deal with objects. 131 00:05:36.230 --> 00:05:38.720 In this code here, we've got my friends list, 132 00:05:38.720 --> 00:05:41.920 which has three strings, Rolf, Jose and Charlie. 133 00:05:41.920 --> 00:05:44.060 And then we've got some friends that are nearby, 134 00:05:44.060 --> 00:05:45.790 Rolf and Anna. 135 00:05:45.790 --> 00:05:47.400 And notice these two are lists, 136 00:05:47.400 --> 00:05:49.760 and then I'm doing friends dot intersection 137 00:05:49.760 --> 00:05:51.260 of friends nearby. 138 00:05:51.260 --> 00:05:53.440 Intersection is something we can do with sets. 139 00:05:53.440 --> 00:05:54.550 If you recall, 140 00:05:54.550 --> 00:05:56.690 we looked at this earlier on in the course. 141 00:05:56.690 --> 00:05:58.560 It's not something you can do with lists. 142 00:05:58.560 --> 00:06:01.180 So then you get an Attribute Error, 143 00:06:01.180 --> 00:06:04.600 the list object has no attribute intersection. 144 00:06:04.600 --> 00:06:06.460 You can't do intersection 145 00:06:06.460 --> 00:06:08.540 because the list doesn't know what it is. 146 00:06:08.540 --> 00:06:10.350 So this is a nicer error message. 147 00:06:10.350 --> 00:06:13.127 It tells you what object you're trying to use. 148 00:06:13.127 --> 00:06:14.931 And this gives a list object. 149 00:06:14.931 --> 00:06:17.663 And it tells you what you tried to do that you cannot do. 150 00:06:17.663 --> 00:06:21.330 In this case use the intersection attribute. 151 00:06:22.350 --> 00:06:23.830 Okay. 152 00:06:23.830 --> 00:06:25.500 This is a pretty nice error when you get it. 153 00:06:25.500 --> 00:06:28.493 You see okay, I messed up, I used the wrong thing there. 154 00:06:30.070 --> 00:06:32.230 We've got Not Implemented Error. 155 00:06:32.230 --> 00:06:35.170 And this is an error that you won't be seeing very often, 156 00:06:35.170 --> 00:06:37.283 but it's an error you can actually use. 157 00:06:38.350 --> 00:06:40.920 And here's how you can use it. 158 00:06:40.920 --> 00:06:42.760 We've got a class that has an init method, 159 00:06:42.760 --> 00:06:44.370 a constructor. 160 00:06:44.370 --> 00:06:45.750 Let's examine a username and a password 161 00:06:45.750 --> 00:06:48.563 and then it sets them and then it's got a login method. 162 00:06:49.530 --> 00:06:51.770 But we've not worked on the login method yet, 163 00:06:51.770 --> 00:06:53.063 so it doesn't work yet. 164 00:06:53.930 --> 00:06:57.470 Instead of returning none or doing nothing, 165 00:06:57.470 --> 00:07:00.023 We can instead raise a Not Implemented Error. 166 00:07:01.080 --> 00:07:03.107 And this is the syntax for raising an error. 167 00:07:03.107 --> 00:07:04.750 We're going to look at this in more detail 168 00:07:04.750 --> 00:07:06.570 in the next couple of videos. 169 00:07:06.570 --> 00:07:07.780 And then inside the brackets, 170 00:07:07.780 --> 00:07:10.450 you can put a small description saying something like this, 171 00:07:10.450 --> 00:07:12.820 feature has not been implemented yet. 172 00:07:12.820 --> 00:07:15.300 What's going to happen is when somebody calls this method, 173 00:07:15.300 --> 00:07:17.200 they're going to get an error. 174 00:07:17.200 --> 00:07:19.440 And it's going to say, Not Implemented Error. 175 00:07:19.440 --> 00:07:21.500 This feature has not been implemented yet. 176 00:07:21.500 --> 00:07:25.640 And then it's going to say when you called user dot login. 177 00:07:25.640 --> 00:07:27.740 So it's a pretty nice arrow to raise 178 00:07:27.740 --> 00:07:30.733 when you've not yet gotten around to implement something. 179 00:07:32.730 --> 00:07:33.920 A Runtime Error. 180 00:07:33.920 --> 00:07:35.780 Runtime error is a pretty generic error. 181 00:07:35.780 --> 00:07:37.350 And 182 00:07:37.350 --> 00:07:39.850 it's not terribly clear when it happens. 183 00:07:39.850 --> 00:07:41.463 I've seen it a couple of times. 184 00:07:42.440 --> 00:07:46.210 But it's not a error you'll be seeing very often at all. 185 00:07:46.210 --> 00:07:49.870 This is essentially a base error, a base class. 186 00:07:49.870 --> 00:07:52.660 And other arrows inherit from this one. 187 00:07:52.660 --> 00:07:56.360 Others extend this one to be Runtime Errors. 188 00:07:56.360 --> 00:07:58.150 A Runtime Error is just an error that happens 189 00:07:58.150 --> 00:07:59.370 when you're running your programme. 190 00:07:59.370 --> 00:08:00.820 So that's basically anything. 191 00:08:02.160 --> 00:08:03.480 We've got Syntax Error. 192 00:08:03.480 --> 00:08:07.310 Syntax Error is when we mess up on Python. 193 00:08:07.310 --> 00:08:08.880 So here we've got somebody messing up 194 00:08:08.880 --> 00:08:10.387 and defining their class user, 195 00:08:10.387 --> 00:08:12.280 can you see, can you spot the, 196 00:08:12.280 --> 00:08:14.370 the missing thing? 197 00:08:14.370 --> 00:08:18.100 For me, this is like big blank, 198 00:08:18.100 --> 00:08:19.660 where something should be. 199 00:08:19.660 --> 00:08:23.400 But of course for you, it may not be so obvious for now. 200 00:08:23.400 --> 00:08:25.540 And as you get more familiar with Python, 201 00:08:25.540 --> 00:08:26.420 as you programme more, 202 00:08:26.420 --> 00:08:28.240 and you encounter more of these errors, 203 00:08:28.240 --> 00:08:31.210 this is going to start becoming a bigger and bigger 204 00:08:31.210 --> 00:08:32.653 and bigger missing thing. 205 00:08:33.580 --> 00:08:35.690 Which is the colon. 206 00:08:35.690 --> 00:08:37.860 The colon is missing at the end of the class. 207 00:08:37.860 --> 00:08:40.710 This is illegal syntax, because you've got class user, 208 00:08:40.710 --> 00:08:42.620 and then it expects a colon afterwards. 209 00:08:42.620 --> 00:08:44.300 So you'll get a Syntax Error, 210 00:08:44.300 --> 00:08:46.223 the same illegal syntax. 211 00:08:47.810 --> 00:08:50.400 Then we've got Indentation Error fairly related, 212 00:08:50.400 --> 00:08:52.990 also a problem with syntax in Python. 213 00:08:52.990 --> 00:08:56.340 And the error could arise from something like this. 214 00:08:56.340 --> 00:08:58.510 You defined a function at two x and y, 215 00:08:58.510 --> 00:08:59.870 you put a call on after. 216 00:08:59.870 --> 00:09:02.950 And remember the block of the function has to be indented, 217 00:09:02.950 --> 00:09:05.210 it has to have at least two spaces in front, 218 00:09:05.210 --> 00:09:07.130 preferably four spaces. 219 00:09:07.130 --> 00:09:11.130 But this return x plus y doesn't have any spaces in front. 220 00:09:11.130 --> 00:09:12.610 So you'd get an Indentation Error. 221 00:09:12.610 --> 00:09:15.290 And it would say something like block, 222 00:09:15.290 --> 00:09:16.780 indentation block expected, 223 00:09:16.780 --> 00:09:19.530 after function definition or something like that. 224 00:09:19.530 --> 00:09:22.910 So basically, you can do this, if you want, 225 00:09:22.910 --> 00:09:25.170 you can indent and put pass, 226 00:09:25.170 --> 00:09:28.130 and that just means this function does nothing. 227 00:09:28.130 --> 00:09:30.950 And then you can put the return x plus y at the bottom. 228 00:09:30.950 --> 00:09:32.510 But of course this, 229 00:09:32.510 --> 00:09:33.770 this would also be an error. 230 00:09:33.770 --> 00:09:35.570 Do you know which error it would be? 231 00:09:37.900 --> 00:09:40.330 I hope you got that, it would be a name error, 232 00:09:40.330 --> 00:09:42.910 because of course x and y would not be defined. 233 00:09:42.910 --> 00:09:45.010 They are only defined within the function. 234 00:09:45.010 --> 00:09:47.200 Remember the concept of scope. 235 00:09:47.200 --> 00:09:49.180 They do not live outside the function, 236 00:09:49.180 --> 00:09:50.900 so as soon as the function ends, 237 00:09:50.900 --> 00:09:52.440 x and y no longer exists, 238 00:09:52.440 --> 00:09:55.390 you're using them here outside the function. 239 00:09:55.390 --> 00:09:59.120 Because the function body is only the indented block. 240 00:09:59.120 --> 00:10:00.320 So this will give you a Name error 241 00:10:00.320 --> 00:10:01.740 You're trying to use x and y. 242 00:10:01.740 --> 00:10:03.340 Two things that are not defined. 243 00:10:04.890 --> 00:10:06.200 Then we've got a Tab Error. 244 00:10:06.200 --> 00:10:08.670 A Tab Error is a pretty frequent error 245 00:10:08.670 --> 00:10:10.540 when you switch between editors. 246 00:10:10.540 --> 00:10:12.890 So just be careful when you're switching between editors 247 00:10:12.890 --> 00:10:15.140 that this doesn't happen. 248 00:10:15.140 --> 00:10:18.110 Here we've got an add two function. 249 00:10:18.110 --> 00:10:20.460 And then the indented body 250 00:10:20.460 --> 00:10:22.090 has been indented with four spaces. 251 00:10:22.090 --> 00:10:25.630 That's what that little square bracket means. 252 00:10:25.630 --> 00:10:28.920 And that square bracket on the bottom of the line, 253 00:10:28.920 --> 00:10:31.970 space, space, space, space, return, x plus y. 254 00:10:31.970 --> 00:10:33.130 So there's four spaces there. 255 00:10:33.130 --> 00:10:37.310 Totally legitimate Python syntax there, 256 00:10:37.310 --> 00:10:38.800 you can indent using four spaces. 257 00:10:38.800 --> 00:10:41.510 But then notice how the pow function has been defined 258 00:10:41.510 --> 00:10:44.887 using a tab character at the start instead of four spaces. 259 00:10:44.887 --> 00:10:46.300 And Python doesn't really like it 260 00:10:46.300 --> 00:10:48.950 when you mix and match indentation types. 261 00:10:48.950 --> 00:10:52.660 So I'm actually not sure if this would work. 262 00:10:52.660 --> 00:10:54.590 But certainly within a function, 263 00:10:54.590 --> 00:10:57.170 do not mix and match types. 264 00:10:57.170 --> 00:10:59.780 Try to not mix and match indentation types at all, 265 00:10:59.780 --> 00:11:01.290 in any of your programmes. 266 00:11:01.290 --> 00:11:04.260 Decide now whether you want to use tabs or spaces, 267 00:11:04.260 --> 00:11:07.313 and stick to that, at least within a project. 268 00:11:09.000 --> 00:11:10.970 So again, these are spaces. 269 00:11:10.970 --> 00:11:13.133 These are tabs, don't mix them. 270 00:11:15.860 --> 00:11:17.550 By the way, and let's go back to this. 271 00:11:17.550 --> 00:11:19.930 And I will quickly just mention the... 272 00:11:21.040 --> 00:11:21.873 pow function, what it does, 273 00:11:21.873 --> 00:11:24.740 because I don't think we've encountered this syntax before. 274 00:11:24.740 --> 00:11:27.980 We've got two arguments n and x, 275 00:11:27.980 --> 00:11:31.170 and then is returning n, times, times x. 276 00:11:31.170 --> 00:11:33.873 And times, times in Python just means to the power of. 277 00:11:34.750 --> 00:11:37.490 And so that's n raised to the x. 278 00:11:37.490 --> 00:11:40.333 For example, two to the three is eight, and so forth. 279 00:11:41.320 --> 00:11:42.860 Pretty nifty syntax there in Python, 280 00:11:42.860 --> 00:11:44.010 I don't think we've encountered that, 281 00:11:44.010 --> 00:11:45.680 just wanted to mention it. 282 00:11:45.680 --> 00:11:47.423 Let's go over to a Type Error. 283 00:11:48.330 --> 00:11:50.090 The Type Error is pretty common. 284 00:11:50.090 --> 00:11:52.540 And a type error can happen when you do something like this. 285 00:11:52.540 --> 00:11:54.580 In the first line we're adding five plus five, 286 00:11:54.580 --> 00:11:56.977 and that gives us 10, as we would expect. 287 00:11:56.977 --> 00:11:59.440 In the second line we're adding hi plus ha, 288 00:11:59.440 --> 00:12:01.620 and it's given to us hiha, as you would expect, 289 00:12:01.620 --> 00:12:03.700 cause it's concatenating the strings together. 290 00:12:03.700 --> 00:12:05.660 And then we're doing something that you should not do, 291 00:12:05.660 --> 00:12:09.000 which is add a string and a number, 292 00:12:09.000 --> 00:12:09.900 an integer. 293 00:12:09.900 --> 00:12:12.440 Do not do that for that will be a Type Error. 294 00:12:12.440 --> 00:12:15.390 You've given the plus function, 295 00:12:15.390 --> 00:12:20.060 which is a function, two types that it does not support. 296 00:12:20.060 --> 00:12:22.940 So both of these things should be the same type. 297 00:12:22.940 --> 00:12:24.470 And you've given it a name and a string, 298 00:12:24.470 --> 00:12:26.533 and it says unsupported operand types. 299 00:12:27.400 --> 00:12:31.680 By the way, the plus symbol is actually a function, 300 00:12:31.680 --> 00:12:34.960 it's a Dunder function like lan. 301 00:12:34.960 --> 00:12:37.250 We will look into that later on, 302 00:12:37.250 --> 00:12:39.450 and how you can use it. 303 00:12:39.450 --> 00:12:42.050 But the int and the string object 304 00:12:42.050 --> 00:12:45.610 have the plus Dunder function defined. 305 00:12:45.610 --> 00:12:48.130 And it says that you cannot add them two together. 306 00:12:48.130 --> 00:12:50.460 So then you get a Type Error. 307 00:12:50.460 --> 00:12:52.060 Notice that you get the Type error 308 00:12:52.060 --> 00:12:57.060 because somebody defined the int and sdr classes 309 00:12:57.390 --> 00:12:59.830 as raising this Type Error, 310 00:12:59.830 --> 00:13:03.050 when you pass in different types. 311 00:13:03.050 --> 00:13:04.400 It doesn't just happen magically, 312 00:13:04.400 --> 00:13:06.250 somebody has actually written the code 313 00:13:06.250 --> 00:13:09.250 to give you this error when you do this. 314 00:13:09.250 --> 00:13:11.060 And so just remember, there's no magic here. 315 00:13:11.060 --> 00:13:12.380 It's all programming. 316 00:13:12.380 --> 00:13:15.123 Somebody has written the code that raises this error. 317 00:13:16.640 --> 00:13:18.070 Let's move on to Value Error. 318 00:13:18.070 --> 00:13:19.727 Value Error is also very common. 319 00:13:19.727 --> 00:13:23.880 It happens when you give beating functions 320 00:13:24.780 --> 00:13:28.160 a value of the correct type, 321 00:13:28.160 --> 00:13:32.370 but sorry, a value of the correct type, potentially, 322 00:13:32.370 --> 00:13:33.943 but incorrect value. 323 00:13:34.880 --> 00:13:36.130 So here's an example. 324 00:13:36.130 --> 00:13:38.700 We are turning into an int, a string, 325 00:13:38.700 --> 00:13:41.100 which is normally a fine thing to do. 326 00:13:41.100 --> 00:13:44.400 But the string cannot contain decimal places. 327 00:13:44.400 --> 00:13:45.430 If it does, 328 00:13:45.430 --> 00:13:48.980 it says hey, you should really convert this into a float, 329 00:13:48.980 --> 00:13:50.230 and that's what you should do. 330 00:13:50.230 --> 00:13:52.110 So here we're calling the int function 331 00:13:52.110 --> 00:13:53.950 with 20.5 as a string, 332 00:13:53.950 --> 00:13:55.710 and it says Value Error, 333 00:13:55.710 --> 00:13:58.820 invalid literal for int with base 10, 20.5. 334 00:13:58.820 --> 00:14:01.420 20.5 is not something you can give the int function. 335 00:14:02.700 --> 00:14:06.890 Normally only built in functions will raise a Value Error. 336 00:14:06.890 --> 00:14:08.910 If you want to raise Value Errors, 337 00:14:08.910 --> 00:14:11.050 I recommend that you create your own errors, 338 00:14:11.050 --> 00:14:14.400 which we're going to look at later on in this section. 339 00:14:14.400 --> 00:14:16.230 Further we've got Import Error. 340 00:14:16.230 --> 00:14:18.090 An Import Error is also really popular 341 00:14:18.090 --> 00:14:20.200 and is a big pain in the ass. 342 00:14:20.200 --> 00:14:22.350 And here's when it happens. 343 00:14:22.350 --> 00:14:24.593 You've got an import blog. 344 00:14:25.840 --> 00:14:27.340 Inside app dot py, 345 00:14:27.340 --> 00:14:30.010 and then you've got blog dot py say 346 00:14:30.010 --> 00:14:32.360 that from app imports menu. 347 00:14:32.360 --> 00:14:35.740 Let's imagine you want to run the menu inside blog or 348 00:14:36.660 --> 00:14:38.500 you know, maybe app has some sort of variable 349 00:14:38.500 --> 00:14:40.550 you want to import or something. 350 00:14:40.550 --> 00:14:44.780 So what's happening here is that app is importing blog. 351 00:14:44.780 --> 00:14:46.770 Blog is importing app. 352 00:14:46.770 --> 00:14:48.300 And what happens when you import? 353 00:14:48.300 --> 00:14:50.280 Well you go over to the file you're imported. 354 00:14:50.280 --> 00:14:51.520 And you run through it quickly, 355 00:14:51.520 --> 00:14:55.340 define the functions, execute on imports and so forth. 356 00:14:55.340 --> 00:14:58.280 And once that's done, you go back to the original file 357 00:14:58.280 --> 00:15:00.930 and you continue from right after the import. 358 00:15:00.930 --> 00:15:04.380 So importing blog actually runs blog dot py. 359 00:15:04.380 --> 00:15:06.060 What happened when you run blog dot py? 360 00:15:06.060 --> 00:15:09.620 Well it has to go over to app dot py and run that. 361 00:15:09.620 --> 00:15:11.150 The first line there is import blog. 362 00:15:11.150 --> 00:15:13.490 So it has to go back to blog and import app. 363 00:15:13.490 --> 00:15:15.170 And then it goes back to blog and then so forth. 364 00:15:15.170 --> 00:15:16.780 This is a big circle. 365 00:15:16.780 --> 00:15:20.720 And what happens is circular imports. 366 00:15:20.720 --> 00:15:21.790 This is what they're called. 367 00:15:21.790 --> 00:15:24.070 Importing one file which imports the same file. 368 00:15:24.070 --> 00:15:25.600 You'll get a circular import. 369 00:15:25.600 --> 00:15:27.160 And this doesn't necessarily have to happen 370 00:15:27.160 --> 00:15:29.170 from between two files. 371 00:15:29.170 --> 00:15:31.940 You can have file a that imports file b 372 00:15:31.940 --> 00:15:34.363 that imports file c, that imports file a. 373 00:15:35.230 --> 00:15:37.310 So you can have a circular import with many files, 374 00:15:37.310 --> 00:15:39.180 and it can be a bit of a hassle. 375 00:15:39.180 --> 00:15:41.490 So just be careful with your imports. 376 00:15:41.490 --> 00:15:42.940 You'll get an Import Error, 377 00:15:42.940 --> 00:15:44.480 and it will say something around the lines 378 00:15:44.480 --> 00:15:46.950 of circular import here. 379 00:15:46.950 --> 00:15:48.700 So be careful if you get an Import Error, 380 00:15:48.700 --> 00:15:49.930 ask a question in the course q & a, 381 00:15:49.930 --> 00:15:51.783 will be very happy to help you out. 382 00:15:53.370 --> 00:15:56.190 Finally, let's look at Deprecation Warning. 383 00:15:56.190 --> 00:15:59.240 Deprecation Warning is as it says there, a warning, 384 00:15:59.240 --> 00:16:00.620 not an error. 385 00:16:00.620 --> 00:16:02.720 However, Python treats it the same way. 386 00:16:02.720 --> 00:16:05.380 So if you produce one, 387 00:16:05.380 --> 00:16:07.030 if you raise one, 388 00:16:07.030 --> 00:16:08.720 your programme will still crash. 389 00:16:08.720 --> 00:16:13.720 But it's meant to mean, hey, something wrong happened here. 390 00:16:13.740 --> 00:16:16.820 This thing that you're trying to do is deprecated. 391 00:16:16.820 --> 00:16:19.140 And what does deprecated mean? 392 00:16:19.140 --> 00:16:22.980 Deprecated means that it is no longer the best way 393 00:16:22.980 --> 00:16:24.760 of doing something. 394 00:16:24.760 --> 00:16:27.070 There is now a better way of doing whatever it is 395 00:16:27.070 --> 00:16:28.460 you're trying to do. 396 00:16:28.460 --> 00:16:29.830 Let's look at this example code. 397 00:16:29.830 --> 00:16:31.517 We've got usual class, 398 00:16:31.517 --> 00:16:34.870 and at the top we got from database, import, database. 399 00:16:34.870 --> 00:16:37.475 The user class has a constructor. 400 00:16:37.475 --> 00:16:39.390 And then it has two methods, 401 00:16:39.390 --> 00:16:41.680 a register method, which is an instance method, 402 00:16:41.680 --> 00:16:43.530 that writes to the database, 403 00:16:43.530 --> 00:16:45.670 the username and password of the user. 404 00:16:45.670 --> 00:16:48.120 Then it also raises a Deprecation Warning, 405 00:16:48.120 --> 00:16:51.523 and it says, user register still works, but it's deprecated. 406 00:16:52.860 --> 00:16:57.240 Then we've got a class method that is called register user. 407 00:16:57.240 --> 00:16:59.720 And that takes in a username and password. 408 00:16:59.720 --> 00:17:02.300 And writes to the database, a username and password, 409 00:17:02.300 --> 00:17:04.210 and returns an object of type user. 410 00:17:04.210 --> 00:17:05.820 I like this second one better. 411 00:17:05.820 --> 00:17:07.820 That's why I saved this example. 412 00:17:07.820 --> 00:17:09.420 But nonetheless, it was just to show you 413 00:17:09.420 --> 00:17:12.200 that you can raise a Deprecation Warning 414 00:17:12.200 --> 00:17:13.380 in any of your methods, 415 00:17:13.380 --> 00:17:15.810 to tell whomever uses it that hey, 416 00:17:15.810 --> 00:17:19.733 this is no longer the accepted way of doing things. 417 00:17:22.550 --> 00:17:24.203 But, there is a but. 418 00:17:25.070 --> 00:17:27.600 Often you won't be raising any of these exceptions, 419 00:17:27.600 --> 00:17:29.310 except possibly the Deprecation Warning, 420 00:17:29.310 --> 00:17:30.810 probably not even that one. 421 00:17:30.810 --> 00:17:33.150 You'll normally be raising Not Implemented Error, 422 00:17:33.150 --> 00:17:35.150 if you are sort of working on something. 423 00:17:36.200 --> 00:17:38.570 But you won't be raising many of these. 424 00:17:38.570 --> 00:17:40.590 You should create your own exceptions 425 00:17:40.590 --> 00:17:42.193 with even better names. 426 00:17:43.630 --> 00:17:45.680 And we will look at exactly what that means, 427 00:17:45.680 --> 00:17:47.430 and what that better name is, 428 00:17:47.430 --> 00:17:49.990 and how you can create your own exceptions. 429 00:17:49.990 --> 00:17:51.820 So let's look at that right now. 430 00:17:51.820 --> 00:17:53.310 In the very next video. 431 00:17:53.310 --> 00:17:54.460 And I'll see you there.