WEBVTT 1 00:00:01.189 --> 00:00:03.830 so at the end of the last video we 2 00:00:03.830 --> 00:00:06.200 created troll class which if you recall so at the end of the last video we 3 00:00:06.200 --> 00:00:09.679 was a subclass of enemy at the moment created troll class which if you recall 4 00:00:09.679 --> 00:00:11.629 it's not doing anything useful as you was a subclass of enemy at the moment 5 00:00:11.629 --> 00:00:13.519 saw we could just as easily use an enemy it's not doing anything useful as you 6 00:00:13.519 --> 00:00:16.039 instead and get exactly the same result saw we could just as easily use an enemy 7 00:00:16.039 --> 00:00:19.010 so inspired by his brothers fine example instead and get exactly the same result 8 00:00:19.010 --> 00:00:22.010 ugs game or has been going to the gym and so inspired by his brothers fine example 9 00:00:22.010 --> 00:00:24.440 working out in fact he's still a bit of ugs game or has been going to the gym and 10 00:00:24.440 --> 00:00:26.630 a trend and now all the trolls have working out in fact he's still a bit of 11 00:00:26.630 --> 00:00:29.270 been toughened up so every trolls that a trend and now all the trolls have 12 00:00:29.270 --> 00:00:31.280 our adventurers encounter in this game been toughened up so every trolls that 13 00:00:31.280 --> 00:00:35.300 will now take 23 points of damage now our adventurers encounter in this game 14 00:00:35.300 --> 00:00:37.010 because they're mortal only have one will now take 23 points of damage now 15 00:00:37.010 --> 00:00:39.199 life so there's no need to specify because they're mortal only have one 16 00:00:39.199 --> 00:00:42.140 either value when we create them what we life so there's no need to specify 17 00:00:42.140 --> 00:00:44.210 need to do is initialize the troll either value when we create them what we 18 00:00:44.210 --> 00:00:46.609 instances with just a name so that we need to do is initialize the troll 19 00:00:46.609 --> 00:00:48.979 can tell them apart the hit points and instances with just a name so that we 20 00:00:48.979 --> 00:00:50.870 lives will be the same for every troll can tell them apart the hit points and 21 00:00:50.870 --> 00:00:54.199 that we create so going back to our lives will be the same for every troll 22 00:00:54.199 --> 00:00:57.079 enemy class where our trolls are defined that we create so going back to our 23 00:00:57.079 --> 00:01:00.260 at the moment we just got pass in their so enemy class where our trolls are defined 24 00:01:00.260 --> 00:01:01.219 we're going to change that we're going at the moment we just got pass in their so 25 00:01:01.219 --> 00:01:03.859 to actually delete that and then we're we're going to change that we're going 26 00:01:03.859 --> 00:01:05.870 going to come down here we're going to to actually delete that and then we're 27 00:01:05.870 --> 00:01:09.800 add our init.... going to come down here we're going to 28 00:01:09.800 --> 00:01:12.260 .... add our init.... 29 00:01:12.260 --> 00:01:14.870 .... .... 30 00:01:14.870 --> 00:01:19.220 ... .... 31 00:01:19.220 --> 00:01:22.820 ... ... 32 00:01:22.820 --> 00:01:24.260 so the hit points and lives will be the same ... 33 00:01:24.260 --> 00:01:26.660 for every troll we create the name the so the hit points and lives will be the same 34 00:01:26.660 --> 00:01:28.130 instance rather is just going to have a for every troll we create the name the 35 00:01:28.130 --> 00:01:30.260 name and that's why we're passing it as instance rather is just going to have a 36 00:01:30.260 --> 00:01:32.090 a parameter to the constructor to the name and that's why we're passing it as 37 00:01:32.090 --> 00:01:34.190 init method the underscore underscore init a parameter to the constructor to the 38 00:01:34.190 --> 00:01:34.760 init method the underscore underscore init 39 00:01:34.760 --> 00:01:36.710 __ which all here 40 00:01:36.710 --> 00:01:38.060 refer to as init __ which all here 41 00:01:38.060 --> 00:01:40.580 alright so let's try running this and refer to as init 42 00:01:40.580 --> 00:01:41.540 see what happens alright so let's try running this and 43 00:01:41.540 --> 00:01:47.000 alright so the error we're getting here see what happens 44 00:01:47.000 --> 00:01:49.730 on line 3 is missing one required alright so the error we're getting here 45 00:01:49.730 --> 00:01:51.350 positional arguments name on line 3 is missing one required 46 00:01:51.350 --> 00:01:54.110 we've got an error running this code and positional arguments name 47 00:01:54.110 --> 00:01:55.400 if you think about it the reason is we've got an error running this code and 48 00:01:55.400 --> 00:01:56.450 quite clear if you think about it the reason is 49 00:01:56.450 --> 00:01:58.130 that's because we need to pass an quite clear 50 00:01:58.130 --> 00:02:01.730 argument to the troll constructor when that's because we need to pass an 51 00:02:01.730 --> 00:02:03.890 we're creating it so i'm going to do is argument to the troll constructor when 52 00:02:03.890 --> 00:02:06.560 come up here to line 3 and change this we're creating it so i'm going to do is 53 00:02:06.560 --> 00:02:09.019 so it's got troll and we're going to come up here to line 3 and change this 54 00:02:09.019 --> 00:02:12.800 put the name in there pug so let's run so it's got troll and we're going to 55 00:02:12.800 --> 00:02:13.560 that put the name in there pug so let's run 56 00:02:13.560 --> 00:02:17.099 this time we've actually got an error on that 57 00:02:17.099 --> 00:02:19.230 line four and we got one below as well this time we've actually got an error on 58 00:02:19.230 --> 00:02:21.300 which we won't worry about for now but line four and we got one below as well 59 00:02:21.300 --> 00:02:25.200 the call to print on line we are relying on which we won't worry about for now but 60 00:02:25.200 --> 00:02:26.880 the string method that we added to the the call to print on line we are relying on 61 00:02:26.880 --> 00:02:29.310 enemy class that's actually getting an the string method that we added to the 62 00:02:29.310 --> 00:02:31.319 error here on line 4 and also down enemy class that's actually getting an 63 00:02:31.319 --> 00:02:34.980 here on line 18 in the enemy class when error here on line 4 and also down 64 00:02:34.980 --> 00:02:36.030 we tried and that's what we try to here on line 18 in the enemy class when 65 00:02:36.030 --> 00:02:38.130 access the attributes of our troll we tried and that's what we try to 66 00:02:38.130 --> 00:02:40.920 instance now by the way when reading access the attributes of our troll 67 00:02:40.920 --> 00:02:42.540 these stack traces that python instance now by the way when reading 68 00:02:42.540 --> 00:02:44.550 produces i actually like to read them these stack traces that python 69 00:02:44.550 --> 00:02:45.600 from the bottom up produces i actually like to read them 70 00:02:45.600 --> 00:02:47.549 I think they make more sense that way so from the bottom up 71 00:02:47.549 --> 00:02:49.650 starting at the bottom here our I think they make more sense that way so 72 00:02:49.650 --> 00:02:51.840 code raise an attribute starting at the bottom here our 73 00:02:51.840 --> 00:02:54.690 error because our troll object has no code raise an attribute 74 00:02:54.690 --> 00:02:57.840 attribute name reading upwards the error error because our troll object has no 75 00:02:57.840 --> 00:03:01.470 happened at line 18 in enemypy attribute name reading upwards the error 76 00:03:01.470 --> 00:03:03.959 in the string method and that's happened at line 18 in enemypy 77 00:03:03.959 --> 00:03:05.310 where we were trying to access the in the string method and that's 78 00:03:05.310 --> 00:03:07.739 attributes to format them into the where we were trying to access the 79 00:03:07.739 --> 00:03:10.920 string that we want to print and that attributes to format them into the 80 00:03:10.920 --> 00:03:13.440 was called right here on line 4 in main string that we want to print and that 81 00:03:13.440 --> 00:03:14.880 which is why we've got this other error was called right here on line 4 in main 82 00:03:14.880 --> 00:03:16.860 showing that's obviously the which is why we've got this other error 83 00:03:16.860 --> 00:03:18.870 print statement so what's actually showing that's obviously the 84 00:03:18.870 --> 00:03:22.170 happened here so why is our troll lost all print statement so what's actually 85 00:03:22.170 --> 00:03:23.910 the attributes that it just had in the happened here so why is our troll lost all 86 00:03:23.910 --> 00:03:26.519 previous video well the reason is we the attributes that it just had in the 87 00:03:26.519 --> 00:03:28.799 added the init method to the troll class previous video well the reason is we 88 00:03:28.799 --> 00:03:31.350 and when we did that Python longer points the added the init method to the troll class 89 00:03:31.350 --> 00:03:34.470 enemy classes and init method so it runs the and when we did that Python longer points the 90 00:03:34.470 --> 00:03:37.440 init method in our troll class instead so enemy classes and init method so it runs the 91 00:03:37.440 --> 00:03:39.750 if the enemy's init method doesn't run init method in our troll class instead so 92 00:03:39.750 --> 00:03:42.840 then the attributes don't gets set if the enemy's init method doesn't run 93 00:03:42.840 --> 00:03:45.239 so what we actually need to do here is then the attributes don't gets set 94 00:03:45.239 --> 00:03:47.040 call the init method of the superclass so what we actually need to do here is 95 00:03:47.040 --> 00:03:51.569 enemy inside our trolls init method now call the init method of the superclass 96 00:03:51.569 --> 00:03:53.220 there's a few ways to do this and i'll enemy inside our trolls init method now 97 00:03:53.220 --> 00:03:55.829 leave the one I recommend in python 3 to there's a few ways to do this and i'll 98 00:03:55.829 --> 00:03:58.769 last so we'll go back to our enemy class leave the one I recommend in python 3 to 99 00:03:58.769 --> 00:04:02.400 and down here to troll now firstly we last so we'll go back to our enemy class 100 00:04:02.400 --> 00:04:04.590 can actually prefix the call to init and down here to troll now firstly we 101 00:04:04.590 --> 00:04:07.650 with the name of the superclass so we can actually prefix the call to init 102 00:04:07.650 --> 00:04:09.180 can do something like get rid of the with the name of the superclass so we 103 00:04:09.180 --> 00:04:12.420 pass... can do something like get rid of the 104 00:04:12.420 --> 00:04:15.299 .... pass... 105 00:04:15.299 --> 00:04:15.870 ... .... 106 00:04:15.870 --> 00:04:26.370 ... ... 107 00:04:26.370 --> 00:04:30.120 but that would still get an error ... 108 00:04:30.120 --> 00:04:32.220 because we've got to these other lines but that would still get an error 109 00:04:32.220 --> 00:04:35.700 here on lines six and nine got because we've got to these other lines 110 00:04:35.700 --> 00:04:37.410 multiple parameters we're passing the here on lines six and nine got 111 00:04:37.410 --> 00:04:39.480 hit points or both the hit points and the multiple parameters we're passing the 112 00:04:39.480 --> 00:04:42.300 number of lives so let's actually fix hit points or both the hit points and the 113 00:04:42.300 --> 00:04:44.670 those first so their just the names number of lives so let's actually fix 114 00:04:44.670 --> 00:04:46.740 they're just one parameter just to see those first so their just the names 115 00:04:46.740 --> 00:04:52.500 what happens if you run this now the they're just one parameter just to see 116 00:04:52.500 --> 00:04:54.270 program runs and all our trolls you what happens if you run this now the 117 00:04:54.270 --> 00:04:56.820 can see now have 23 hitpoints well program runs and all our trolls you 118 00:04:56.820 --> 00:04:58.050 actually going to say the all do but can see now have 23 hitpoints well 119 00:04:58.050 --> 00:05:00.480 urgh doesn't that's because we're using the actually going to say the all do but 120 00:05:00.480 --> 00:05:03.300 enemy class here instead of troll but if I change urgh doesn't that's because we're using the 121 00:05:03.300 --> 00:05:08.130 that to troll run that again we see now enemy class here instead of troll but if I change 122 00:05:08.130 --> 00:05:10.980 that they all do have one life and 23 hit points that to troll run that again we see now 123 00:05:10.980 --> 00:05:13.169 so if they all have different that they all do have one life and 23 hit points 124 00:05:13.169 --> 00:05:15.540 names so that we can tell them apart so if they all have different 125 00:05:15.540 --> 00:05:17.669 although telling one troll from another names so that we can tell them apart 126 00:05:17.669 --> 00:05:19.710 is a specialized skill it won't be although telling one troll from another 127 00:05:19.710 --> 00:05:21.120 covering that in the course is a specialized skill it won't be 128 00:05:21.120 --> 00:05:23.040 alright so let's go back to the trolll covering that in the course 129 00:05:23.040 --> 00:05:26.400 class enemy.py and lets have a look at alright so let's go back to the trolll 130 00:05:26.400 --> 00:05:29.100 what we did here so we created an init class enemy.py and lets have a look at 131 00:05:29.100 --> 00:05:31.530 method for our troll class and when we did what we did here so we created an init 132 00:05:31.530 --> 00:05:33.960 that the init method of enemy is no method for our troll class and when we did 133 00:05:33.960 --> 00:05:36.419 longer called you could say that the that the init method of enemy is no 134 00:05:36.419 --> 00:05:38.850 trolls init method had shadowed init longer called you could say that the 135 00:05:38.850 --> 00:05:41.880 method in its superclass so to get a trolls init method had shadowed init 136 00:05:41.880 --> 00:05:43.919 around that and make sure that class method in its superclass so to get a 137 00:05:43.919 --> 00:05:46.740 attributes are initialized we called the around that and make sure that class 138 00:05:46.740 --> 00:05:49.560 enemy superclass init method that's the attributes are initialized we called the 139 00:05:49.560 --> 00:05:55.770 call on line 24 by prefixing the method name with the class name now its imporant enemy superclass init method that's the 140 00:05:55.770 --> 00:05:57.210 that we provide the correct number of call on line 24 by prefixing the method name with the class name now its imporant 141 00:05:57.210 --> 00:06:00.060 parameters but because the enemy init that we provide the correct number of 142 00:06:00.060 --> 00:06:02.040 method has default values we could have parameters but because the enemy init 143 00:06:02.040 --> 00:06:03.510 left off one or more of the parameters method has default values we could have 144 00:06:03.510 --> 00:06:05.700 out here this ones here left off one or more of the parameters 145 00:06:05.700 --> 00:06:07.740 you won't always want to provide default out here this ones here 146 00:06:07.740 --> 00:06:09.690 values in your class methods so it's you won't always want to provide default 147 00:06:09.690 --> 00:06:11.010 important to remember that we are values in your class methods so it's 148 00:06:11.010 --> 00:06:12.690 calling the superclass init method important to remember that we are 149 00:06:12.690 --> 00:06:15.030 here and it needs 4 argument's calling the superclass init method 150 00:06:15.030 --> 00:06:18.120 now our trolls init method only takes two here and it needs 4 argument's 151 00:06:18.120 --> 00:06:20.190 arguments so we should provide values now our trolls init method only takes two 152 00:06:20.190 --> 00:06:22.440 for the remaining two alright so that's arguments so we should provide values 153 00:06:22.440 --> 00:06:24.120 one way to do it and that would be the for the remaining two alright so that's 154 00:06:24.120 --> 00:06:27.660 only way available in python 2 now in one way to do it and that would be the 155 00:06:27.660 --> 00:06:30.270 python three we can use the super only way available in python 2 now in 156 00:06:30.270 --> 00:06:32.580 method instead and if you want to cope python three we can use the super 157 00:06:32.580 --> 00:06:34.950 with multiple inheritance you must use method instead and if you want to cope 158 00:06:34.950 --> 00:06:35.969 super with multiple inheritance you must use 159 00:06:35.969 --> 00:06:37.469 instead of specifying the base class super 160 00:06:37.469 --> 00:06:39.989 name so let's do that so i'm going to instead of specifying the base class 161 00:06:39.989 --> 00:06:42.179 change this enemy part here delete that name so let's do that so i'm going to 162 00:06:42.179 --> 00:06:45.809 out we're going to put... change this enemy part here delete that 163 00:06:45.809 --> 00:06:53.309 ... out we're going to put... 164 00:06:53.309 --> 00:06:56.519 ... ... 165 00:06:56.519 --> 00:07:00.059 ....let's just run that to make sure it ... 166 00:07:00.059 --> 00:07:07.110 still works it still works now syntax ....let's just run that to make sure it 167 00:07:07.110 --> 00:07:09.029 does make it a little bit hard to read still works it still works now syntax 168 00:07:09.029 --> 00:07:11.579 but what we're basically doing is passing the does make it a little bit hard to read 169 00:07:11.579 --> 00:07:14.309 name of our class troll and the current but what we're basically doing is passing the 170 00:07:14.309 --> 00:07:17.339 instance self to super and then we're name of our class troll and the current 171 00:07:17.339 --> 00:07:19.709 using that to call the init method of instance self to super and then we're 172 00:07:19.709 --> 00:07:22.019 the base class now when you're using using that to call the init method of 173 00:07:22.019 --> 00:07:24.299 super inside of class instance there's a the base class now when you're using 174 00:07:24.299 --> 00:07:26.639 more readable shorthand way of writing super inside of class instance there's a 175 00:07:26.639 --> 00:07:29.939 unfortunately and we can do away with more readable shorthand way of writing 176 00:07:29.939 --> 00:07:32.669 this troll and self in the parentheses unfortunately and we can do away with 177 00:07:32.669 --> 00:07:35.909 just change it so it's got super with this troll and self in the parentheses 178 00:07:35.909 --> 00:07:37.619 the parentheses dot than the __ just change it so it's got super with 179 00:07:37.619 --> 00:07:40.259 init and that would be the the parentheses dot than the __ 180 00:07:40.259 --> 00:07:42.029 way that I suggest you normally use it init and that would be the 181 00:07:42.029 --> 00:07:43.919 when calling methods in the superclass way that I suggest you normally use it 182 00:07:43.919 --> 00:07:46.589 and it does it the identical thing to when calling methods in the superclass 183 00:07:46.589 --> 00:07:48.209 what we had previously and I will just undo and it does it the identical thing to 184 00:07:48.209 --> 00:07:54.989 that take a copy of that line and cut what we had previously and I will just undo 185 00:07:54.989 --> 00:07:56.759 that out that take a copy of that line and cut 186 00:07:56.759 --> 00:08:00.899 fix it so it's doing the same thing as that out 187 00:08:00.899 --> 00:08:02.879 the commented out line on line 24 fix it so it's doing the same thing as 188 00:08:02.879 --> 00:08:06.029 but the compiler knows the type troll the commented out line on line 24 189 00:08:06.029 --> 00:08:08.339 and the instant self so there's really but the compiler knows the type troll 190 00:08:08.339 --> 00:08:10.229 no need to specify them in the super and the instant self so there's really 191 00:08:10.229 --> 00:08:10.860 call no need to specify them in the super 192 00:08:10.860 --> 00:08:14.399 ok so now our trolls behave differently to a call 193 00:08:14.399 --> 00:08:17.099 basic enemy when we create a new troll ok so now our trolls behave differently to a 194 00:08:17.099 --> 00:08:19.619 we just give him or her a name and hit basic enemy when we create a new troll 195 00:08:19.619 --> 00:08:22.169 points are live are automatically set for we just give him or her a name and hit 196 00:08:22.169 --> 00:08:25.439 us now subclasses can have additional points are live are automatically set for 197 00:08:25.439 --> 00:08:28.529 attributes to extend their behavior and us now subclasses can have additional 198 00:08:28.529 --> 00:08:30.299 allow them to do things that their base attributes to extend their behavior and 199 00:08:30.299 --> 00:08:33.269 class doesn't do so lets demonstrate allow them to do things that their base 200 00:08:33.269 --> 00:08:36.539 that by giving our trolls a grunt method class doesn't do so lets demonstrate 201 00:08:36.539 --> 00:08:39.599 will come down here that by giving our trolls a grunt method 202 00:08:39.599 --> 00:08:45.899 and do... will come down here 203 00:08:45.899 --> 00:08:52.470 .... and do... 204 00:08:52.470 --> 00:09:02.519 ... .... 205 00:09:02.519 --> 00:09:05.730 alright so that's the grunt method added ... 206 00:09:05.730 --> 00:09:07.920 to the troll class so let's get our alright so that's the grunt method added 207 00:09:07.920 --> 00:09:09.269 trolls to grunt by adding a bit more to the troll class so let's get our 208 00:09:09.269 --> 00:09:13.019 code in the main py file.... trolls to grunt by adding a bit more 209 00:09:13.019 --> 00:09:16.380 ... code in the main py file.... 210 00:09:16.380 --> 00:09:24.180 .... ... 211 00:09:24.180 --> 00:09:26.490 ... .... 212 00:09:26.490 --> 00:09:33.899 ... ... 213 00:09:33.899 --> 00:09:38.579 .... ... 214 00:09:38.579 --> 00:09:41.639 ....the .... 215 00:09:41.639 --> 00:09:43.170 troubles with trolls is that once you set ....the 216 00:09:43.170 --> 00:09:45.060 one off that you'll have to join in so troubles with trolls is that once you set 217 00:09:45.060 --> 00:09:46.439 let's go ahead and actually add the code one off that you'll have to join in so 218 00:09:46.439 --> 00:09:49.500 for the others so we'll do another troll.... let's go ahead and actually add the code 219 00:09:49.500 --> 00:09:53.220 .... for the others so we'll do another troll.... 220 00:09:53.220 --> 00:10:00.029 ....and then run that me .... 221 00:10:00.029 --> 00:10:02.339 pug pug stop stomping me ugh ughs ....and then run that me 222 00:10:02.339 --> 00:10:05.519 stomp you stomp you then me urg urgh pug pug stop stomping me ugh ughs 223 00:10:05.519 --> 00:10:08.100 so about now a wise adventure might be stomp you stomp you then me urg urgh 224 00:10:08.100 --> 00:10:10.470 decided that it's time to leave so so about now a wise adventure might be 225 00:10:10.470 --> 00:10:13.139 getting back to our troll sub class decided that it's time to leave so 226 00:10:13.139 --> 00:10:15.389 it can now do things that a basic getting back to our troll sub class 227 00:10:15.389 --> 00:10:17.490 enemy cant and if we try and get an it can now do things that a basic 228 00:10:17.490 --> 00:10:19.410 enemy instance to grant them then will get an enemy cant and if we try and get an 229 00:10:19.410 --> 00:10:21.389 error so just to demonstrate that enemy instance to grant them then will get an 230 00:10:21.389 --> 00:10:28.649 .... error so just to demonstrate that 231 00:10:28.649 --> 00:10:33.329 ... .... 232 00:10:33.329 --> 00:10:36.509 ... ... 233 00:10:36.509 --> 00:10:40.380 ...and if you try to run that not ... 234 00:10:40.380 --> 00:10:42.930 surprising we get an error enemy object ...and if you try to run that not 235 00:10:42.930 --> 00:10:46.290 has no attribute grunt we just highlight surprising we get an error enemy object 236 00:10:46.290 --> 00:10:47.670 that with the mouse unresolved has no attribute grunt we just highlight 237 00:10:47.670 --> 00:10:49.559 attribute reference grunt for class enemy that with the mouse unresolved 238 00:10:49.559 --> 00:10:52.170 which again isn't surprising because it attribute reference grunt for class enemy 239 00:10:52.170 --> 00:10:52.660 hasn't which again isn't surprising because it 240 00:10:52.660 --> 00:10:54.730 the enemy class hasn't got a grunt hasn't 241 00:10:54.730 --> 00:10:55.779 method the enemy class hasn't got a grunt 242 00:10:55.779 --> 00:10:57.790 ok so as well as adding extra behavior method 243 00:10:57.790 --> 00:11:00.490 to our subclasses we can now also change ok so as well as adding extra behavior 244 00:11:00.490 --> 00:11:03.009 the behavior of methods that exists in to our subclasses we can now also change 245 00:11:03.009 --> 00:11:05.920 the superclass to see how to do that the behavior of methods that exists in 246 00:11:05.920 --> 00:11:08.079 we're going to start off by creating a the superclass to see how to do that 247 00:11:08.079 --> 00:11:10.660 new class but let's start working on we're going to start off by creating a 248 00:11:10.660 --> 00:11:17.279 that in the next video new class but let's start working on