WEBVTT 1 00:00:01.420 --> 00:00:03.699 as I mentioned towards the end of the 2 00:00:03.699 --> 00:00:06.460 last video you can also create your own as I mentioned towards the end of the 3 00:00:06.460 --> 00:00:08.920 exceptions their just objects after all last video you can also create your own 4 00:00:08.920 --> 00:00:11.710 so we can provide more immediate exceptions their just objects after all 5 00:00:11.710 --> 00:00:13.690 feedback to the developers using our duck so we can provide more immediate 6 00:00:13.690 --> 00:00:15.580 module so let's go back and have a look feedback to the developers using our duck 7 00:00:15.580 --> 00:00:19.960 at the add_duck method you module so let's go back and have a look 8 00:00:19.960 --> 00:00:22.510 can see that their on line 50 now we at the add_duck method you 9 00:00:22.510 --> 00:00:24.400 provided a hint that it should be can see that their on line 50 now we 10 00:00:24.400 --> 00:00:27.670 passing a duck or subclass of duck now provided a hint that it should be 11 00:00:27.670 --> 00:00:30.010 if we extended the duck class to create passing a duck or subclass of duck now 12 00:00:30.010 --> 00:00:33.519 a mallard class perhaps then a mallard is if we extended the duck class to create 13 00:00:33.519 --> 00:00:36.069 also a duck but we can do better than a mallard class perhaps then a mallard is 14 00:00:36.069 --> 00:00:38.440 this we can check the type of the also a duck but we can do better than 15 00:00:38.440 --> 00:00:41.260 argument that's past and show you how to this we can check the type of the 16 00:00:41.260 --> 00:00:44.649 do that so we're going to type in here argument that's past and show you how to 17 00:00:44.649 --> 00:00:50.289 in the add duck method... do that so we're going to type in here 18 00:00:50.289 --> 00:00:54.579 ... in the add duck method... 19 00:00:54.579 --> 00:00:57.429 ...if we go ... 20 00:00:57.429 --> 00:01:02.469 back to our file again and run it this ...if we go 21 00:01:02.469 --> 00:01:04.750 time we get all seven ducks taking off back to our file again and run it this 22 00:01:04.750 --> 00:01:06.189 and there's no exception time we get all seven ducks taking off 23 00:01:06.189 --> 00:01:09.520 now that's because Percy isn't a dark so and there's no exception 24 00:01:09.520 --> 00:01:13.960 this test on line 51 fails and Percy now that's because Percy isn't a dark so 25 00:01:13.960 --> 00:01:15.820 doesn't get added to the list and this test on line 51 fails and Percy 26 00:01:15.820 --> 00:01:17.079 incidentally make sure you use the doesn't get added to the list and 27 00:01:17.079 --> 00:01:19.840 lowercase D in the first duck this part incidentally make sure you use the 28 00:01:19.840 --> 00:01:23.200 here on line 51 if you used a capital D lowercase D in the first duck this part 29 00:01:23.200 --> 00:01:25.119 there then you checking the type of the here on line 51 if you used a capital D 30 00:01:25.119 --> 00:01:28.539 duck class which is a class so the there then you checking the type of the 31 00:01:28.539 --> 00:01:31.270 type function returns the type duck class which is a class so the 32 00:01:31.270 --> 00:01:33.609 of an object so here we are testing that the type function returns the type 33 00:01:33.609 --> 00:01:35.200 duck that's been passed to our method of an object so here we are testing that the 34 00:01:35.200 --> 00:01:38.020 really is a duck never do this duck that's been passed to our method 35 00:01:38.020 --> 00:01:39.820 now not only is this is not python really is a duck never do this 36 00:01:39.820 --> 00:01:41.740 it's also wrong now not only is this is not python 37 00:01:41.740 --> 00:01:44.499 now it's an obvious thing to do and if it's also wrong 38 00:01:44.499 --> 00:01:46.810 you used to programming in java you now it's an obvious thing to do and if 39 00:01:46.810 --> 00:01:50.319 probably expected to work but to see why you used to programming in java you 40 00:01:50.319 --> 00:01:52.569 it doesn't we're going to create a mallard probably expected to work but to see why 41 00:01:52.569 --> 00:01:57.039 class that extends duck i'm just going it doesn't we're going to create a mallard 42 00:01:57.039 --> 00:01:59.289 to put it just after the penguin class that extends duck i'm just going 43 00:01:59.289 --> 00:02:05.800 class.... to put it just after the penguin 44 00:02:05.800 --> 00:02:11.020 ... class.... 45 00:02:11.020 --> 00:02:13.420 ....if we were really ... 46 00:02:13.420 --> 00:02:16.330 creating a mallard then you add some of ....if we were really 47 00:02:16.330 --> 00:02:19.300 the properties of a mallard but here we're creating a mallard then you add some of 48 00:02:19.300 --> 00:02:20.680 just demonstrating that the previous the properties of a mallard but here we're 49 00:02:20.680 --> 00:02:23.500 code fails with sub classes so going back just demonstrating that the previous 50 00:02:23.500 --> 00:02:26.680 to the migration .py file we need to code fails with sub classes so going back 51 00:02:26.680 --> 00:02:29.260 change your definition and will actually to the migration .py file we need to 52 00:02:29.260 --> 00:02:32.170 change for percy here and we'll put change your definition and will actually 53 00:02:32.170 --> 00:02:35.110 Percy equals duck . mallard this time change for percy here and we'll put 54 00:02:35.110 --> 00:02:42.850 let's run this and you can see that we Percy equals duck . mallard this time 55 00:02:42.850 --> 00:02:44.470 still only get the seven ducks flying let's run this and you can see that we 56 00:02:44.470 --> 00:02:46.720 so this type function that we're talking still only get the seven ducks flying 57 00:02:46.720 --> 00:02:52.810 about now on line 55 returns the exact so this type function that we're talking 58 00:02:52.810 --> 00:02:55.390 type of an object and we check to see if about now on line 55 returns the exact 59 00:02:55.390 --> 00:02:58.510 that type is duck which isn't is mallard type of an object and we check to see if 60 00:02:58.510 --> 00:03:01.780 in this case of the one that we set up that type is duck which isn't is mallard 61 00:03:01.780 --> 00:03:05.590 for percy here on line 11 now if in this case of the one that we set up 62 00:03:05.590 --> 00:03:06.700 you want to do this there are for percy here on line 11 now if 63 00:03:06.700 --> 00:03:09.580 occasionally times when you need to then you want to do this there are 64 00:03:09.580 --> 00:03:12.459 use is instance instead of checking the occasionally times when you need to then 65 00:03:12.459 --> 00:03:14.980 type so let me change this duck method use is instance instead of checking the 66 00:03:14.980 --> 00:03:19.300 now so instead of type... type so let me change this duck method 67 00:03:19.300 --> 00:03:24.430 ... now so instead of type... 68 00:03:24.430 --> 00:03:25.270 ....first one is the lowercase ... 69 00:03:25.270 --> 00:03:28.030 version for the variable the second one ....first one is the lowercase 70 00:03:28.030 --> 00:03:31.060 is the actual class name and if we do version for the variable the second one 71 00:03:31.060 --> 00:03:32.380 that now that we've done that if we is the actual class name and if we do 72 00:03:32.380 --> 00:03:39.970 go back to our code run it again and what I that now that we've done that if we 73 00:03:39.970 --> 00:03:41.230 should have done is remove this last go back to our code run it again and what I 74 00:03:41.230 --> 00:03:44.260 part so all we really want to do at this should have done is remove this last 75 00:03:44.260 --> 00:03:46.030 point is if is instance and then in part so all we really want to do at this 76 00:03:46.030 --> 00:03:48.670 parentheses duck , duck now if i try point is if is instance and then in 77 00:03:48.670 --> 00:03:52.239 run that this time you can see parentheses duck , duck now if i try 78 00:03:52.239 --> 00:03:54.640 we've actually got all 8 ducks in the run that this time you can see 79 00:03:54.640 --> 00:03:55.150 air we've actually got all 8 ducks in the 80 00:03:55.150 --> 00:03:57.610 the reason for that is this is air 81 00:03:57.610 --> 00:04:01.090 is instance function so again if you must the reason for that is this is 82 00:04:01.090 --> 00:04:02.860 do this type of checking always uses this is is instance function so again if you must 83 00:04:02.860 --> 00:04:05.170 instance or there's also is instance do this type of checking always uses this is 84 00:04:05.170 --> 00:04:06.670 method if you're dealing with classes instance or there's also is instance 85 00:04:06.670 --> 00:04:09.459 rather than instances of classes and method if you're dealing with classes 86 00:04:09.459 --> 00:04:11.560 don't check the type like we did earlier rather than instances of classes and 87 00:04:11.560 --> 00:04:13.390 now keep in mind though it's still not don't check the type like we did earlier 88 00:04:13.390 --> 00:04:16.720 pythonic though in Python remember we're now keep in mind though it's still not 89 00:04:16.720 --> 00:04:18.190 not interested in whether percy is a pythonic though in Python remember we're 90 00:04:18.190 --> 00:04:19.030 duck or not not interested in whether percy is a 91 00:04:19.030 --> 00:04:20.769 we're only interested in wether percy duck or not 92 00:04:20.769 --> 00:04:23.530 can fly if it flies like a duck it's a we're only interested in wether percy 93 00:04:23.530 --> 00:04:26.139 duck now checking like this again can fly if it flies like a duck it's a 94 00:04:26.139 --> 00:04:28.570 is sometimes appropriate if your duck now checking like this again 95 00:04:28.570 --> 00:04:30.340 function or method requires and int for is sometimes appropriate if your 96 00:04:30.340 --> 00:04:32.800 example it does make sense to check if function or method requires and int for 97 00:04:32.800 --> 00:04:35.770 you've got an int or subclass of int and example it does make sense to check if 98 00:04:35.770 --> 00:04:37.870 yes in Python you can subclassing if you've got an int or subclass of int and 99 00:04:37.870 --> 00:04:40.870 you want to it wouldn't be practical to yes in Python you can subclassing if 100 00:04:40.870 --> 00:04:43.690 check all the behavior of an argument to you want to it wouldn't be practical to 101 00:04:43.690 --> 00:04:46.300 see if it quacks like an int but when check all the behavior of an argument to 102 00:04:46.300 --> 00:04:48.190 dealing with the most other objects the see if it quacks like an int but when 103 00:04:48.190 --> 00:04:50.770 pythonic way is to focus on the behavior dealing with the most other objects the 104 00:04:50.770 --> 00:04:54.940 you want not on the type of the object so pythonic way is to focus on the behavior 105 00:04:54.940 --> 00:04:58.810 what we really want to do is check to you want not on the type of the object so 106 00:04:58.810 --> 00:05:02.410 see if the arguments gotta fly method if what we really want to do is check to 107 00:05:02.410 --> 00:05:04.570 it can fly then we can add it to the see if the arguments gotta fly method if 108 00:05:04.570 --> 00:05:07.060 flock so there's two steps to this first it can fly then we can add it to the 109 00:05:07.060 --> 00:05:08.530 what we want to do is check if there's flock so there's two steps to this first 110 00:05:08.530 --> 00:05:11.200 an attribute called fly the next we what we want to do is check if there's 111 00:05:11.200 --> 00:05:13.930 check to see that this is a method and an attribute called fly the next we 112 00:05:13.930 --> 00:05:16.570 not a data attribute so let's add the check to see that this is a method and 113 00:05:16.570 --> 00:05:18.580 code for that instead not a data attribute so let's add the 114 00:05:18.580 --> 00:05:20.050 so again we're doing this code on that code for that instead 115 00:05:20.050 --> 00:05:23.020 line 55 going to change that line again so again we're doing this code on that 116 00:05:23.020 --> 00:05:25.900 so the first line of the add_ line 55 going to change that line again 117 00:05:25.900 --> 00:05:28.600 duck method we're going to put.... so the first line of the add_ 118 00:05:28.600 --> 00:05:32.590 ... duck method we're going to put.... 119 00:05:32.590 --> 00:05:53.080 ... ... 120 00:05:53.080 --> 00:05:56.110 ....so the get ... 121 00:05:56.110 --> 00:05:58.539 attributed method checks an objects ....so the get 122 00:05:58.539 --> 00:06:00.820 dictionary to see if it contains the attributed method checks an objects 123 00:06:00.820 --> 00:06:03.400 attribute we specify fly in this case dictionary to see if it contains the 124 00:06:03.400 --> 00:06:06.010 now if the attribute wasn't present attribute we specify fly in this case 125 00:06:06.010 --> 00:06:08.650 would get an error so it works very now if the attribute wasn't present 126 00:06:08.650 --> 00:06:11.440 like a dictionaries get method by would get an error so it works very 127 00:06:11.440 --> 00:06:13.660 specifying a default value we get none like a dictionaries get method by 128 00:06:13.660 --> 00:06:15.640 returned instead of the error specifying a default value we get none 129 00:06:15.640 --> 00:06:17.860 next we're using this callable returned instead of the error 130 00:06:17.860 --> 00:06:20.140 function to check if the fly method is next we're using this callable 131 00:06:20.140 --> 00:06:22.510 callable now functions and methods are function to check if the fly method is 132 00:06:22.510 --> 00:06:25.900 callable data attributes aren't now that's callable now functions and methods are 133 00:06:25.900 --> 00:06:27.820 not strictly true because you can't have callable data attributes aren't now that's 134 00:06:27.820 --> 00:06:30.940 a callable data attribute in python but not strictly true because you can't have 135 00:06:30.940 --> 00:06:32.650 if we call the fly attribute a callable data attribute in python but 136 00:06:32.650 --> 00:06:35.020 then our code will work so this test is if we call the fly attribute 137 00:06:35.020 --> 00:06:37.750 perfectly fine and just to confirm if we then our code will work so this test is 138 00:06:37.750 --> 00:06:41.470 go back and run it again we are getting perfectly fine and just to confirm if we 139 00:06:41.470 --> 00:06:43.120 all 8 ducks flying go back and run it again we are getting 140 00:06:43.120 --> 00:06:45.310 alright so let's change our percy now all 8 ducks flying 141 00:06:45.310 --> 00:06:50.530 back to being a penguin again and to alright so let's change our percy now 142 00:06:50.530 --> 00:06:53.500 confirm this time you run it again we back to being a penguin again and to 143 00:06:53.500 --> 00:06:55.330 now getting seven ducks taking off confirm this time you run it again we 144 00:06:55.330 --> 00:06:57.160 so clearly the fact that we have now getting seven ducks taking off 145 00:06:57.160 --> 00:07:00.490 change percy back to penguin means that so clearly the fact that we have 146 00:07:00.490 --> 00:07:02.800 the code that we've defined here is change percy back to penguin means that 147 00:07:02.800 --> 00:07:04.810 correctly figuring out that the penguin the code that we've defined here is 148 00:07:04.810 --> 00:07:07.810 class hasn't got a fly method and correctly figuring out that the penguin 149 00:07:07.810 --> 00:07:09.460 not trying to call it and and class hasn't got a fly method and 150 00:07:09.460 --> 00:07:11.350 consequently not trying to add it to our not trying to call it and and 151 00:07:11.350 --> 00:07:14.050 list so now that we've done that this is consequently not trying to add it to our 152 00:07:14.050 --> 00:07:16.720 the pythonic way to do the test we're list so now that we've done that this is 153 00:07:16.720 --> 00:07:18.010 not interested in the type of the duck the pythonic way to do the test we're 154 00:07:18.010 --> 00:07:20.740 argument we are only interested in whether it not interested in the type of the duck 155 00:07:20.740 --> 00:07:22.120 can fly or not argument we are only interested in whether it 156 00:07:22.120 --> 00:07:24.130 now for our purposes if it flies like a duck can fly or not 157 00:07:24.130 --> 00:07:26.139 then its a duck now for our purposes if it flies like a duck 158 00:07:26.139 --> 00:07:27.910 now sometimes this will be an then its a duck 159 00:07:27.910 --> 00:07:30.849 appropriate solution as it is often you now sometimes this will be an 160 00:07:30.849 --> 00:07:32.349 want to notify the caller that appropriate solution as it is often you 161 00:07:32.349 --> 00:07:34.599 something's going wrong and the Penguins want to notify the caller that 162 00:07:34.599 --> 00:07:37.240 been excluded from the flock now this is something's going wrong and the Penguins 163 00:07:37.240 --> 00:07:39.789 really a programming error the user of been excluded from the flock now this is 164 00:07:39.789 --> 00:07:41.440 our module shouldn't be passing really a programming error the user of 165 00:07:41.440 --> 00:07:44.169 flightless birds to a module that deals our module shouldn't be passing 166 00:07:44.169 --> 00:07:47.380 with bird migration so this may be flightless birds to a module that deals 167 00:07:47.380 --> 00:07:49.479 another place where raising an exception with bird migration so this may be 168 00:07:49.479 --> 00:07:52.419 is actually appropriate let's go ahead another place where raising an exception 169 00:07:52.419 --> 00:07:54.729 and do that so if we don't add the duck is actually appropriate let's go ahead 170 00:07:54.729 --> 00:07:56.440 to the flocks list let's raise an and do that so if we don't add the duck 171 00:07:56.440 --> 00:07:58.659 exception at that point we're going to to the flocks list let's raise an 172 00:07:58.659 --> 00:08:01.659 do is put an else to the if callable exception at that point we're going to 173 00:08:01.659 --> 00:08:06.729 ... do is put an else to the if callable 174 00:08:06.729 --> 00:08:12.699 .... ... 175 00:08:12.699 --> 00:08:27.380 .... .... 176 00:08:27.380 --> 00:08:29.270 ....so again what we're .... 177 00:08:29.270 --> 00:08:31.580 doing here is if we don't add the duck to ....so again what we're 178 00:08:31.580 --> 00:08:33.260 the flocks list because we established doing here is if we don't add the duck to 179 00:08:33.260 --> 00:08:35.930 that because we established that it hasn't got an the flocks list because we established 180 00:08:35.930 --> 00:08:38.360 attribute called fly and that it's not that because we established that it hasn't got an 181 00:08:38.360 --> 00:08:40.789 callable then we're going to raise this attribute called fly and that it's not 182 00:08:40.789 --> 00:08:43.969 typeerror we saw earlier that there are callable then we're going to raise this 183 00:08:43.969 --> 00:08:46.279 a lot whole load of exceptions available typeerror we saw earlier that there are 184 00:08:46.279 --> 00:08:48.140 so you should try and choose the most a lot whole load of exceptions available 185 00:08:48.140 --> 00:08:50.870 appropriate exception to raise here so you should try and choose the most 186 00:08:50.870 --> 00:08:52.399 they're passing something that can't fly appropriate exception to raise here 187 00:08:52.399 --> 00:08:56.779 so a type error is a good choice and is they're passing something that can't fly 188 00:08:56.779 --> 00:08:58.580 also shows a valid use of the type so a type error is a good choice and is 189 00:08:58.580 --> 00:09:01.310 function so we're not going to check what type also shows a valid use of the type 190 00:09:01.310 --> 00:09:03.470 something is but to provide function so we're not going to check what type 191 00:09:03.470 --> 00:09:05.120 information on the type of object that something is but to provide 192 00:09:05.120 --> 00:09:08.600 caused the problem in Python if you find information on the type of object that 193 00:09:08.600 --> 00:09:10.279 yourself using type to check what type caused the problem in Python if you find 194 00:09:10.279 --> 00:09:12.500 something is then step back and consider yourself using type to check what type 195 00:09:12.500 --> 00:09:14.240 if there's another way to achieve what something is then step back and consider 196 00:09:14.240 --> 00:09:16.430 you're trying to do so now if there's another way to achieve what 197 00:09:16.430 --> 00:09:17.959 I've done that we go back to migration you're trying to do so now 198 00:09:17.959 --> 00:09:21.920 and run it again you can see we've got I've done that we go back to migration 199 00:09:21.920 --> 00:09:24.740 an exception here cannot add duck are you and run it again you can see we've got 200 00:09:24.740 --> 00:09:26.510 sure it's not a penguin and probably an exception here cannot add duck are you 201 00:09:26.510 --> 00:09:31.100 got an extra space that I could fix up just to sure it's not a penguin and probably 202 00:09:31.100 --> 00:09:33.529 be clear run again got an extra space that I could fix up just to 203 00:09:33.529 --> 00:09:36.380 okay so we are now getting an exception be clear run again 204 00:09:36.380 --> 00:09:39.260 raised before the migration is started okay so we are now getting an exception 205 00:09:39.260 --> 00:09:41.450 at the time the flightless penguin is raised before the migration is started 206 00:09:41.450 --> 00:09:43.880 added to the list now the stack trace at the time the flightless penguin is 207 00:09:43.880 --> 00:09:46.940 here is useful and shows where the added to the list now the stack trace 208 00:09:46.940 --> 00:09:49.070 exception was raised as well as the call here is useful and shows where the 209 00:09:49.070 --> 00:09:52.459 that originated the error now this error exception was raised as well as the call 210 00:09:52.459 --> 00:09:54.200 would be picked up straightaway testing that originated the error now this error 211 00:09:54.200 --> 00:09:56.240 and the programmer can modify the code would be picked up straightaway testing 212 00:09:56.240 --> 00:09:58.760 to remove the bug so it's really and the programmer can modify the code 213 00:09:58.760 --> 00:10:01.130 easy to stick to the Python principle of to remove the bug so it's really 214 00:10:01.130 --> 00:10:03.170 if it quacks like a duck while still easy to stick to the Python principle of 215 00:10:03.170 --> 00:10:04.850 making functions and methods type safe if it quacks like a duck while still 216 00:10:04.850 --> 00:10:08.149 for one of a better expression now making functions and methods type safe 217 00:10:08.149 --> 00:10:09.680 there's another reason you may want to for one of a better expression now 218 00:10:09.680 --> 00:10:11.870 raise exceptions and that's in order to there's another reason you may want to 219 00:10:11.870 --> 00:10:15.140 test your exception handlers now to see raise exceptions and that's in order to 220 00:10:15.140 --> 00:10:16.790 this working we're going to fix the test your exception handlers now to see 221 00:10:16.790 --> 00:10:18.649 code so that Percy does have a fly this working we're going to fix the 222 00:10:18.649 --> 00:10:20.839 method in fact i'm going to give him an code so that Percy does have a fly 223 00:10:20.839 --> 00:10:23.450 aviat method instead and it'll be method in fact i'm going to give him an 224 00:10:23.450 --> 00:10:24.829 clear why shortly aviat method instead and it'll be 225 00:10:24.829 --> 00:10:29.990 let's go back and do that we're going to clear why shortly 226 00:10:29.990 --> 00:10:31.459 add another method down here for the let's go back and do that we're going to 227 00:10:31.459 --> 00:10:35.820 penguin class... add another method down here for the 228 00:10:35.820 --> 00:10:41.670 .... penguin class... 229 00:10:41.670 --> 00:11:09.690 .... .... 230 00:11:09.690 --> 00:11:15.330 now going to explain a bit more about .... 231 00:11:15.330 --> 00:11:16.800 that but first i'm going to run the now going to explain a bit more about 232 00:11:16.800 --> 00:11:19.350 program to make sure that works let's do that but first i'm going to run the 233 00:11:19.350 --> 00:11:24.960 that you can see that that's work fine program to make sure that works let's do 234 00:11:24.960 --> 00:11:26.190 we've got I won the lottery and bought that you can see that that's work fine 235 00:11:26.190 --> 00:11:27.930 a lear jet and haven't got an exception we've got I won the lottery and bought 236 00:11:27.930 --> 00:11:30.060 anymore and going back to our code a lear jet and haven't got an exception 237 00:11:30.060 --> 00:11:32.550 looking at the init method here on our anymore and going back to our code 238 00:11:32.550 --> 00:11:35.700 line 35 we created a new data looking at the init method here on our 239 00:11:35.700 --> 00:11:38.730 attribute called fly and we assign it to our line 35 we created a new data 240 00:11:38.730 --> 00:11:41.220 reference to the aviate method and I attribute called fly and we assign it to our 241 00:11:41.220 --> 00:11:42.570 mentioned not to include the opening and reference to the aviate method and I 242 00:11:42.570 --> 00:11:45.060 closing parenthesis after aviate and mentioned not to include the opening and 243 00:11:45.060 --> 00:11:47.160 noting that IntelliJ actually tried to closing parenthesis after aviate and 244 00:11:47.160 --> 00:11:49.110 add them for us which would have noting that IntelliJ actually tried to 245 00:11:49.110 --> 00:11:50.970 prevented this from working properly so add them for us which would have 246 00:11:50.970 --> 00:11:52.920 by removing the parentheses we're adding prevented this from working properly so 247 00:11:52.920 --> 00:11:55.800 just a reference to the aviate method that by removing the parentheses we're adding 248 00:11:55.800 --> 00:11:59.250 this code here on line 36 now everything just a reference to the aviate method that 249 00:11:59.250 --> 00:12:01.770 in python is an object even functions this code here on line 36 now everything 250 00:12:01.770 --> 00:12:04.230 and methods so you can assign a method in python is an object even functions 251 00:12:04.230 --> 00:12:06.600 to a variable then call that variable and methods so you can assign a method 252 00:12:06.600 --> 00:12:09.240 self.fly in this case just like you to a variable then call that variable 253 00:12:09.240 --> 00:12:11.520 called a method that points to now we self.fly in this case just like you 254 00:12:11.520 --> 00:12:13.290 saw that when we created the deal_ called a method that points to now we 255 00:12:13.290 --> 00:12:15.330 _dealer and deal_player saw that when we created the deal_ 256 00:12:15.330 --> 00:12:17.670 functions and assign them to the command _dealer and deal_player 257 00:12:17.670 --> 00:12:20.250 attribute or our tk buttons all the way back in functions and assign them to the command 258 00:12:20.250 --> 00:12:22.830 the blackjack game in Section 11 attribute or our tk buttons all the way back in 259 00:12:22.830 --> 00:12:25.410 alright so back to raising exceptions for the blackjack game in Section 11 260 00:12:25.410 --> 00:12:28.500 testing we've now got a working program alright so back to raising exceptions for 261 00:12:28.500 --> 00:12:31.620 that contains exception handlers so how testing we've now got a working program 262 00:12:31.620 --> 00:12:33.300 do we test that the exception handlers that contains exception handlers so how 263 00:12:33.300 --> 00:12:34.740 are working correctly do we test that the exception handlers 264 00:12:34.740 --> 00:12:37.110 well with a program like this we could are working correctly 265 00:12:37.110 --> 00:12:39.030 do it by passing inappropriate arguments well with a program like this we could 266 00:12:39.030 --> 00:12:41.220 to the add_duck method and do it by passing inappropriate arguments 267 00:12:41.220 --> 00:12:43.350 we've seen that working but sometimes to the add_duck method and 268 00:12:43.350 --> 00:12:45.630 getting real exceptions isn't that easy we've seen that working but sometimes 269 00:12:45.630 --> 00:12:47.779 simulating something like getting real exceptions isn't that easy 270 00:12:47.779 --> 00:12:49.939 perhaps a disk full exception that simulating something like 271 00:12:49.939 --> 00:12:52.399 prevents writing data to a file is possible perhaps a disk full exception that 272 00:12:52.399 --> 00:12:54.379 but very fiddly to set up prevents writing data to a file is possible 273 00:12:54.379 --> 00:12:55.759 you may also want to respond to but very fiddly to set up 274 00:12:55.759 --> 00:12:57.560 exceptions if the network link goes down you may also want to respond to 275 00:12:57.560 --> 00:12:59.899 while you're downloading data and if you exceptions if the network link goes down 276 00:12:59.899 --> 00:13:02.629 have a test team then probably they will do while you're downloading data and if you 277 00:13:02.629 --> 00:13:03.980 things like switching off the router have a test team then probably they will do 278 00:13:03.980 --> 00:13:05.749 while downloading to make sure your things like switching off the router 279 00:13:05.749 --> 00:13:07.790 program kept gracefully like situations while downloading to make sure your 280 00:13:07.790 --> 00:13:10.490 like that but before it gets to the test program kept gracefully like situations 281 00:13:10.490 --> 00:13:12.559 team you can test your exception like that but before it gets to the test 282 00:13:12.559 --> 00:13:15.680 handling by raising your own exception in team you can test your exception 283 00:13:15.680 --> 00:13:17.360 the code that's protected by a try block handling by raising your own exception in 284 00:13:17.360 --> 00:13:20.089 so going back to our migrate method of the code that's protected by a try block 285 00:13:20.089 --> 00:13:27.889 the flock in here let's now raise an so going back to our migrate method of 286 00:13:27.889 --> 00:13:31.699 exception inside the for loop i'm coming the flock in here let's now raise an 287 00:13:31.699 --> 00:13:34.670 down here to the try then the ducks duck exception inside the for loop i'm coming 288 00:13:34.670 --> 00:13:38.629 . fly and after here what we want to do is down here to the try then the ducks duck 289 00:13:38.629 --> 00:13:41.930 you want to put raise attribute error . fly and after here what we want to do is 290 00:13:41.930 --> 00:13:48.589 ... you want to put raise attribute error 291 00:13:48.589 --> 00:13:55.750 .... ... 292 00:13:55.750 --> 00:14:02.320 ....if we test this now you can see that .... 293 00:14:02.320 --> 00:14:05.920 i'm basically getting this exception ....if we test this now you can see that 294 00:14:05.920 --> 00:14:10.990 its raise after each duck flies and we i'm basically getting this exception 295 00:14:10.990 --> 00:14:12.850 can see the one duck down message after its raise after each duck flies and we 296 00:14:12.850 --> 00:14:13.930 each duck can see the one duck down message after 297 00:14:13.930 --> 00:14:15.850 once all the ducks has been process the each duck 298 00:14:15.850 --> 00:14:17.530 exception is raised and then get the once all the ducks has been process the 299 00:14:17.530 --> 00:14:20.050 stack trace that's a useful technique exception is raised and then get the 300 00:14:20.050 --> 00:14:23.020 for testing your exception handlers just stack trace that's a useful technique 301 00:14:23.020 --> 00:14:24.700 raise an exception that you're handling for testing your exception handlers just 302 00:14:24.700 --> 00:14:28.060 in the code inside the try block and raise an exception that you're handling 303 00:14:28.060 --> 00:14:32.320 again that's this code here on line 73 now in the code inside the try block and 304 00:14:32.320 --> 00:14:34.000 one final thing before I end this video again that's this code here on line 73 now 305 00:14:34.000 --> 00:14:36.610 if you forget about that test exception one final thing before I end this video 306 00:14:36.610 --> 00:14:38.920 and release your code things aren't going to if you forget about that test exception 307 00:14:38.920 --> 00:14:42.760 go well so what you can do is get and release your code things aren't going to 308 00:14:42.760 --> 00:14:44.620 IntelliJ to help make sure you remember go well so what you can do is get 309 00:14:44.620 --> 00:14:46.810 to remove the test code and the way to IntelliJ to help make sure you remember 310 00:14:46.810 --> 00:14:48.520 do that would be to put a comment on the to remove the test code and the way to 311 00:14:48.520 --> 00:14:54.940 line hash... do that would be to put a comment on the 312 00:14:54.940 --> 00:14:58.750 ....adding a comment that contains line hash... 313 00:14:58.750 --> 00:15:00.370 the word to do and it's not ....adding a comment that contains 314 00:15:00.370 --> 00:15:02.950 case-sensitive causes IntelliJ to track the word to do and it's not 315 00:15:02.950 --> 00:15:04.960 it as an item that needs to be done case-sensitive causes IntelliJ to track 316 00:15:04.960 --> 00:15:07.120 note that the comment from the to do it as an item that needs to be done 317 00:15:07.120 --> 00:15:09.820 here appears in a different color depending note that the comment from the to do 318 00:15:09.820 --> 00:15:12.430 on your choice of theme it'll be here appears in a different color depending 319 00:15:12.430 --> 00:15:14.470 probably blue or in my case it's sort of on your choice of theme it'll be 320 00:15:14.470 --> 00:15:17.470 a yellow so that's the to do but probably blue or in my case it's sort of 321 00:15:17.470 --> 00:15:19.660 also what we can do is we can bring up a a yellow so that's the to do but 322 00:15:19.660 --> 00:15:22.030 tab to see all the to dos in our also what we can do is we can bring up a 323 00:15:22.030 --> 00:15:23.680 projects i'm close down the run window tab to see all the to dos in our 324 00:15:23.680 --> 00:15:26.680 this is to do down here that down the projects i'm close down the run window 325 00:15:26.680 --> 00:15:28.750 bottom left click on that and that this is to do down here that down the 326 00:15:28.750 --> 00:15:31.450 actually shows us all that to do entries bottom left click on that and that 327 00:15:31.450 --> 00:15:33.400 and then clicking on them to be taken to actually shows us all that to do entries 328 00:15:33.400 --> 00:15:35.110 the relevant bit of code so even though and then clicking on them to be taken to 329 00:15:35.110 --> 00:15:36.910 it's on the screen i can click on there the relevant bit of code so even though 330 00:15:36.910 --> 00:15:38.710 and it just shows on the screen pretty it's on the screen i can click on there 331 00:15:38.710 --> 00:15:39.310 cool and it just shows on the screen pretty 332 00:15:39.310 --> 00:15:41.170 alright so that's the end of our cool 333 00:15:41.170 --> 00:15:43.660 discussion on exceptions in python in alright so that's the end of our 334 00:15:43.660 --> 00:15:44.770 the next video we're going to get back discussion on exceptions in python in 335 00:15:44.770 --> 00:15:47.080 to databases but we are gonna start making the next video we're going to get back 336 00:15:47.080 --> 00:15:48.760 use of exception handling in our code to databases but we are gonna start making 337 00:15:48.760 --> 00:15:55.260 see you in the next video use of exception handling in our code