WEBVTT 1 00:00:01.750 --> 00:00:04.779 in the last video we saw how subclasses 2 00:00:04.779 --> 00:00:06.970 can take advantage of all the attributes in the last video we saw how subclasses 3 00:00:06.970 --> 00:00:10.030 data attributes and methods of their can take advantage of all the attributes 4 00:00:10.030 --> 00:00:13.000 superclass and we also added extra data attributes and methods of their 5 00:00:13.000 --> 00:00:15.639 functionality to our subclasses to give superclass and we also added extra 6 00:00:15.639 --> 00:00:17.410 them extra attributes that their base functionality to our subclasses to give 7 00:00:17.410 --> 00:00:20.350 class didn't have in this video we're them extra attributes that their base 8 00:00:20.350 --> 00:00:22.180 going to see how to change the behavior class didn't have in this video we're 9 00:00:22.180 --> 00:00:24.880 further so that a subclass can implement going to see how to change the behavior 10 00:00:24.880 --> 00:00:27.430 a method from its superclass and further so that a subclass can implement 11 00:00:27.430 --> 00:00:30.130 actually do things differently now vampire a method from its superclass and 12 00:00:30.130 --> 00:00:32.830 are shifty creatures they don't get to actually do things differently now vampire 13 00:00:32.830 --> 00:00:34.870 live for all eternity without learning a are shifty creatures they don't get to 14 00:00:34.870 --> 00:00:36.969 few tricks after all so what we're going live for all eternity without learning a 15 00:00:36.969 --> 00:00:39.100 to do is change the take damage method few tricks after all so what we're going 16 00:00:39.100 --> 00:00:42.520 so that vampires can dodge when attacked to do is change the take damage method 17 00:00:42.520 --> 00:00:44.950 can turn into a bat or move super fast so that vampires can dodge when attacked 18 00:00:44.950 --> 00:00:48.039 or whatever trick they learnt can turn into a bat or move super fast 19 00:00:48.039 --> 00:00:50.800 to avoid getting bashed so going to or whatever trick they learnt 20 00:00:50.800 --> 00:00:52.989 choose a random element so that our to avoid getting bashed so going to 21 00:00:52.989 --> 00:00:55.300 vampire has a one-in-three chance of choose a random element so that our 22 00:00:55.300 --> 00:00:56.649 dodging a blow vampire has a one-in-three chance of 23 00:00:56.649 --> 00:00:58.719 now we've used the random module before dodging a blow 24 00:00:58.719 --> 00:01:00.879 and we're going to use it again here to now we've used the random module before 25 00:01:00.879 --> 00:01:03.280 generate a random number between one on and we're going to use it again here to 26 00:01:03.280 --> 00:01:05.170 the three inclusive and if we get a generate a random number between one on 27 00:01:05.170 --> 00:01:07.930 three vampires successfully dodges the blow the three inclusive and if we get a 28 00:01:07.930 --> 00:01:11.890 let's open up the vampire class which is three vampires successfully dodges the blow 29 00:01:11.890 --> 00:01:13.420 if you recall is in our enemy.py let's open up the vampire class which is 30 00:01:13.420 --> 00:01:15.850 script we're going to come down here and if you recall is in our enemy.py 31 00:01:15.850 --> 00:01:20.259 start to make some changes so we're script we're going to come down here and 32 00:01:20.259 --> 00:01:24.220 going to do is start and create the start to make some changes so we're 33 00:01:24.220 --> 00:01:28.240 dodges method so.... going to do is start and create the 34 00:01:28.240 --> 00:01:33.040 .... dodges method so.... 35 00:01:33.040 --> 00:01:35.470 .... .... 36 00:01:35.470 --> 00:01:41.259 ... .... 37 00:01:41.259 --> 00:02:14.950 ... ... 38 00:02:14.950 --> 00:02:18.280 ...so we now ... 39 00:02:18.280 --> 00:02:20.200 got a method which is quite simple ...so we now 40 00:02:20.200 --> 00:02:22.540 and if the number generated is three got a method which is quite simple 41 00:02:22.540 --> 00:02:24.220 we're going to print a message that the and if the number generated is three 42 00:02:24.220 --> 00:02:26.140 vampire Dodges and return true we're going to print a message that the 43 00:02:26.140 --> 00:02:28.990 otherwise it returns false now one vampire Dodges and return true 44 00:02:28.990 --> 00:02:30.220 thing you might be wondering is looking otherwise it returns false now one 45 00:02:30.220 --> 00:02:33.250 at code is why i imported the random thing you might be wondering is looking 46 00:02:33.250 --> 00:02:36.010 method on line 43 into this method rather at code is why i imported the random 47 00:02:36.010 --> 00:02:38.200 than putting it at the top of the module method on line 43 into this method rather 48 00:02:38.200 --> 00:02:40.210 and to be honest I'm actually wondering than putting it at the top of the module 49 00:02:40.210 --> 00:02:42.790 that myself and seriously though what i and to be honest I'm actually wondering 50 00:02:42.790 --> 00:02:45.640 have done is show you that it can be that myself and seriously though what i 51 00:02:45.640 --> 00:02:47.830 done there so you may see it in code have done is show you that it can be 52 00:02:47.830 --> 00:02:49.750 that you have to maintain and in rare done there so you may see it in code 53 00:02:49.750 --> 00:02:52.690 cases it may even make sense to do it that you have to maintain and in rare 54 00:02:52.690 --> 00:02:53.920 like this cases it may even make sense to do it 55 00:02:53.920 --> 00:02:55.510 now with that said that most of the time like this 56 00:02:55.510 --> 00:02:57.940 I suggest you don't do it like this i'm now with that said that most of the time 57 00:02:57.940 --> 00:02:59.380 going to take my own advice and remove the I suggest you don't do it like this i'm 58 00:02:59.380 --> 00:03:01.840 import to the start of the module we're going to take my own advice and remove the 59 00:03:01.840 --> 00:03:07.120 it belongs ok write at to the top there import to the start of the module we're 60 00:03:07.120 --> 00:03:13.810 ok so we now defined the Dodges it belongs ok write at to the top there 61 00:03:13.810 --> 00:03:16.570 method for the vampire added an extra ok so we now defined the Dodges 62 00:03:16.570 --> 00:03:18.370 few lines their just for clarity with method for the vampire added an extra 63 00:03:18.370 --> 00:03:20.950 subtitles but i'll remove that too at few lines their just for clarity with 64 00:03:20.950 --> 00:03:21.820 some point subtitles but i'll remove that too at 65 00:03:21.820 --> 00:03:24.340 alright so that's better at this point some point 66 00:03:24.340 --> 00:03:26.980 we now no longer import the random module alright so that's better at this point 67 00:03:26.980 --> 00:03:29.709 every time the Dodge method is called and we now no longer import the random module 68 00:03:29.709 --> 00:03:30.970 if we need to use random numbers in every time the Dodge method is called and 69 00:03:30.970 --> 00:03:33.070 another enemy class object it's now if we need to use random numbers in 70 00:03:33.070 --> 00:03:36.489 available to all the adventure games are another enemy class object it's now 71 00:03:36.489 --> 00:03:38.770 traditionally played with dice so we available to all the adventure games are 72 00:03:38.770 --> 00:03:40.120 will most certainly want to include traditionally played with dice so we 73 00:03:40.120 --> 00:03:43.060 random behavior elsewhere and even if we knew will most certainly want to include 74 00:03:43.060 --> 00:03:44.980 we only ever use the random module random behavior elsewhere and even if we knew 75 00:03:44.980 --> 00:03:46.780 the Dodge method it's still not a good we only ever use the random module 76 00:03:46.780 --> 00:03:48.820 idea to perform the import their the Dodge method it's still not a good 77 00:03:48.820 --> 00:03:50.830 alright so now we're going to change the idea to perform the import their 78 00:03:50.830 --> 00:03:53.860 behavior of our vampires when their alright so now we're going to change the 79 00:03:53.860 --> 00:03:56.019 attacked but before we do that let's behavior of our vampires when their 80 00:03:56.019 --> 00:03:57.880 swing back over to the main python script attacked but before we do that let's 81 00:03:57.880 --> 00:04:01.030 and test our dodged method so swing back over to the main python script 82 00:04:01.030 --> 00:04:03.340 we're going to put some code down here and test our dodged method so 83 00:04:03.340 --> 00:04:05.960 so before line 28 we're going to put some code down here 84 00:04:05.960 --> 00:04:10.190 we're going to put.... so before line 28 85 00:04:10.190 --> 00:04:15.740 .... we're going to put.... 86 00:04:15.740 --> 00:04:19.100 .... .... 87 00:04:19.100 --> 00:04:22.670 ... .... 88 00:04:22.670 --> 00:04:24.170 so lets just run this to see that it does work ... 89 00:04:24.170 --> 00:04:30.110 we can see if we scroll up that vlads so lets just run this to see that it does work 90 00:04:30.110 --> 00:04:32.570 doggers I think it should fix that we can see if we scroll up that vlads 91 00:04:32.570 --> 00:04:36.500 dodges that is better try that doggers I think it should fix that 92 00:04:36.500 --> 00:04:38.780 again and we should see now that vlad our dodges that is better try that 93 00:04:38.780 --> 00:04:41.690 vampires should randomly dodge again and we should see now that vlad our 94 00:04:41.690 --> 00:04:45.080 dodge there and it's a vampires should randomly dodge 95 00:04:45.080 --> 00:04:48.440 one-in-three chance just run it one more time dodge there and it's a 96 00:04:48.440 --> 00:04:53.870 theirs another dodge so we can say one-in-three chance just run it one more time 97 00:04:53.870 --> 00:04:55.490 that we are getting different results it is theirs another dodge so we can say 98 00:04:55.490 --> 00:04:57.200 random and we can see that vlad our that we are getting different results it is 99 00:04:57.200 --> 00:04:59.360 vampire is dodging some of the attacks random and we can see that vlad our 100 00:04:59.360 --> 00:05:02.090 so it does seem to be working we saw vampire is dodging some of the attacks 101 00:05:02.090 --> 00:05:03.680 that the last time I run it there so it does seem to be working we saw 102 00:05:03.680 --> 00:05:05.360 the Dodges came up less i think there's that the last time I run it there 103 00:05:05.360 --> 00:05:07.490 any actually one dodge there for the the Dodges came up less i think there's 104 00:05:07.490 --> 00:05:09.410 whole run and you will find situations any actually one dodge there for the 105 00:05:09.410 --> 00:05:11.870 because it's random where that vlad whole run and you will find situations 106 00:05:11.870 --> 00:05:14.540 won't dodge at all and it's also possible because it's random where that vlad 107 00:05:14.540 --> 00:05:16.520 for him to dodge every blow and never die won't dodge at all and it's also possible 108 00:05:16.520 --> 00:05:19.160 but it's actually not very likely for him to dodge every blow and never die 109 00:05:19.160 --> 00:05:21.140 alright so we've added a new method to but it's actually not very likely 110 00:05:21.140 --> 00:05:22.610 the vampire class to give them the alright so we've added a new method to 111 00:05:22.610 --> 00:05:25.310 ability to dodge what we really want is the vampire class to give them the 112 00:05:25.310 --> 00:05:27.020 for that behavior to be built into the ability to dodge what we really want is 113 00:05:27.020 --> 00:05:29.240 class rather than our main program for that behavior to be built into the 114 00:05:29.240 --> 00:05:30.980 having to check that during each attack class rather than our main program 115 00:05:30.980 --> 00:05:33.740 so to do that we're going to override having to check that during each attack 116 00:05:33.740 --> 00:05:34.490 the take so to do that we're going to override 117 00:05:34.490 --> 00:05:36.800 _damage method that the vampire the take 118 00:05:36.800 --> 00:05:39.920 class inherits from enemy now don't _damage method that the vampire 119 00:05:39.920 --> 00:05:42.560 confuse overriding a method with class inherits from enemy now don't 120 00:05:42.560 --> 00:05:44.750 overloading it as i mentioned earlier confuse overriding a method with 121 00:05:44.750 --> 00:05:48.320 overloading doesn't exist in python so overloading it as i mentioned earlier 122 00:05:48.320 --> 00:05:49.760 if you're not sure what it is then don't overloading doesn't exist in python so 123 00:05:49.760 --> 00:05:51.560 worry because you can't actually do it if you're not sure what it is then don't 124 00:05:51.560 --> 00:05:54.770 anyway but overriding a method that worry because you can't actually do it 125 00:05:54.770 --> 00:05:58.040 means replacing it with another one now anyway but overriding a method that 126 00:05:58.040 --> 00:06:00.800 in languages such as Java and C++ there means replacing it with another one now 127 00:06:00.800 --> 00:06:03.320 are restrictions on the definition of an in languages such as Java and C++ there 128 00:06:03.320 --> 00:06:06.500 overriding method in Python you just write are restrictions on the definition of an 129 00:06:06.500 --> 00:06:08.540 a new method with the same name as the overriding method in Python you just write 130 00:06:08.540 --> 00:06:10.550 one that you want to override the new a new method with the same name as the 131 00:06:10.550 --> 00:06:11.690 method doesn't even have to have the one that you want to override the new 132 00:06:11.690 --> 00:06:13.640 same or accept the same number of method doesn't even have to have the 133 00:06:13.640 --> 00:06:17.480 parameters although it usually will so same or accept the same number of 134 00:06:17.480 --> 00:06:19.729 lets override the enemy parameters although it usually will so 135 00:06:19.729 --> 00:06:21.830 classes take damage method to make lets override the enemy 136 00:06:21.830 --> 00:06:23.779 it do something different in the vampire classes take damage method to make 137 00:06:23.779 --> 00:06:28.189 class going to come up and work on our it do something different in the vampire 138 00:06:28.189 --> 00:06:29.599 vampire class and what we're going to do class going to come up and work on our 139 00:06:29.599 --> 00:06:31.249 is come down here and add another vampire class and what we're going to do 140 00:06:31.249 --> 00:06:37.039 method we're going to type... is come down here and add another 141 00:06:37.039 --> 00:06:38.569 ... method we're going to type... 142 00:06:38.569 --> 00:06:45.710 ... ... 143 00:06:45.710 --> 00:06:48.529 .... ... 144 00:06:48.529 --> 00:06:50.659 ... .... 145 00:06:50.659 --> 00:06:52.399 ... ... 146 00:06:52.399 --> 00:06:54.949 ...let's just run it like I have ... 147 00:06:54.949 --> 00:06:57.830 defined it on line 52 and see what ...let's just run it like I have 148 00:06:57.830 --> 00:07:04.099 happens when I run it and see we defined it on line 52 and see what 149 00:07:04.099 --> 00:07:04.699 can get happens when I run it and see we 150 00:07:04.699 --> 00:07:06.229 or we are getting into an infinite loop so can get 151 00:07:06.229 --> 00:07:11.029 i want to stop the program so let's or we are getting into an infinite loop so 152 00:07:11.029 --> 00:07:12.020 actually go back and look at what i want to stop the program so let's 153 00:07:12.020 --> 00:07:16.279 happened now in our main py script we actually go back and look at what 154 00:07:16.279 --> 00:07:18.020 keep on looping as long as the vamp is happened now in our main py script we 155 00:07:18.020 --> 00:07:21.620 alive on line 27 that method the method keep on looping as long as the vamp is 156 00:07:21.620 --> 00:07:23.449 that can result in an enemy no longer alive on line 27 that method the method 157 00:07:23.449 --> 00:07:25.580 being alive is the take_damage that can result in an enemy no longer 158 00:07:25.580 --> 00:07:28.430 method in the enemy class so when our being alive is the take_damage 159 00:07:28.430 --> 00:07:31.219 vampire object use that method which method in the enemy class so when our 160 00:07:31.219 --> 00:07:33.020 they inherit from enemy the program vampire object use that method which 161 00:07:33.020 --> 00:07:35.689 works but now that we've given the they inherit from enemy the program 162 00:07:35.689 --> 00:07:38.029 vampire class its own take_ works but now that we've given the 163 00:07:38.029 --> 00:07:40.159 damage method that's the method being vampire class its own take_ 164 00:07:40.159 --> 00:07:43.189 called up here on line 29 the new method damage method that's the method being 165 00:07:43.189 --> 00:07:45.740 that we defined for the vampire now called up here on line 29 the new method 166 00:07:45.740 --> 00:07:47.209 because the vampire version of the that we defined for the vampire now 167 00:07:47.209 --> 00:07:48.860 method doesn't do anything yet because the vampire version of the 168 00:07:48.860 --> 00:07:51.439 therefore our vampire never dies now method doesn't do anything yet 169 00:07:51.439 --> 00:07:53.029 that may be a feature of vampires but therefore our vampire never dies now 170 00:07:53.029 --> 00:07:54.439 it's going to make for a very boring that may be a feature of vampires but 171 00:07:54.439 --> 00:07:57.649 game but it does show now that we it's going to make for a very boring 172 00:07:57.649 --> 00:07:59.389 are calling the vampire class take_damage game but it does show now that we 173 00:07:59.389 --> 00:08:02.209 method rather than the enemy are calling the vampire class take_damage 174 00:08:02.209 --> 00:08:04.099 one it really does confirm that that method method rather than the enemy 175 00:08:04.099 --> 00:08:05.509 is being executed and not the original one it really does confirm that that method 176 00:08:05.509 --> 00:08:07.999 take_damage method from the is being executed and not the original 177 00:08:07.999 --> 00:08:09.559 superclass which in this case take_damage method from the 178 00:08:09.559 --> 00:08:12.169 was the enemy class that's why i ran it superclass which in this case 179 00:08:12.169 --> 00:08:13.370 at this stage just to make that clear was the enemy class that's why i ran it 180 00:08:13.370 --> 00:08:16.279 but what we want to do here is call the super at this stage just to make that clear 181 00:08:16.279 --> 00:08:18.800 version of take damage but change things but what we want to do here is call the super 182 00:08:18.800 --> 00:08:21.289 slightly so that our vampires can still version of take damage but change things 183 00:08:21.289 --> 00:08:23.689 dodge now we've already seen how to call slightly so that our vampires can still 184 00:08:23.689 --> 00:08:26.539 a super method from a subclass when we dodge now we've already seen how to call 185 00:08:26.539 --> 00:08:28.610 created the init method for the troll a super method from a subclass when we 186 00:08:28.610 --> 00:08:30.889 and vampire classes so now we can do the created the init method for the troll 187 00:08:30.889 --> 00:08:33.490 same thing for take_damage and vampire classes so now we can do the 188 00:08:33.490 --> 00:08:35.919 we only want to take damage if the same thing for take_damage 189 00:08:35.919 --> 00:08:38.169 vampire doesn't dodge so let's write the we only want to take damage if the 190 00:08:38.169 --> 00:08:40.959 code for that so going back to our enemy vampire doesn't dodge so let's write the 191 00:08:40.959 --> 00:08:43.839 class to our vampire class whether i'm code for that so going back to our enemy 192 00:08:43.839 --> 00:08:46.600 going to get rid of this pass instead what class to our vampire class whether i'm 193 00:08:46.600 --> 00:08:48.850 we're going to put in here is.... going to get rid of this pass instead what 194 00:08:48.850 --> 00:08:54.339 .... we're going to put in here is.... 195 00:08:54.339 --> 00:08:59.740 ... .... 196 00:08:59.740 --> 00:09:04.300 ...so we are using super just like we did ... 197 00:09:04.300 --> 00:09:06.550 when we call these superclasses init ...so we are using super just like we did 198 00:09:06.550 --> 00:09:09.940 method you can see that on line 43 only when we call these superclasses init 199 00:09:09.940 --> 00:09:11.680 this time we are calling the take_ method you can see that on line 43 only 200 00:09:11.680 --> 00:09:14.380 damage method now by calling take_ this time we are calling the take_ 201 00:09:14.380 --> 00:09:16.480 damage like this we don't to write all damage method now by calling take_ 202 00:09:16.480 --> 00:09:18.940 the code again we just wrap the call damage like this we don't to write all 203 00:09:18.940 --> 00:09:21.399 to the super method in a test to see the code again we just wrap the call 204 00:09:21.399 --> 00:09:23.350 whether the vampire dodges or should to the super method in a test to see 205 00:09:23.350 --> 00:09:26.020 take damage the other advantage is now whether the vampire dodges or should 206 00:09:26.020 --> 00:09:27.430 we don't need to put the testing in our take damage the other advantage is now 207 00:09:27.430 --> 00:09:31.060 main program let's put that back so we we don't need to put the testing in our 208 00:09:31.060 --> 00:09:32.649 can take out this if not vamped main program let's put that back so we 209 00:09:32.649 --> 00:09:37.209 Dodges now back to what it was and i can take out this if not vamped 210 00:09:37.209 --> 00:09:39.670 just comment that out of print for now Dodges now back to what it was and i 211 00:09:39.670 --> 00:09:42.940 if we run this we should see that it just comment that out of print for now 212 00:09:42.940 --> 00:09:46.390 works vlad is down eventually if we run this we should see that it 213 00:09:46.390 --> 00:09:48.040 and his got heaps dodging you can see these works vlad is down eventually 214 00:09:48.040 --> 00:09:49.930 are quire a few Dodges this time this and his got heaps dodging you can see these 215 00:09:49.930 --> 00:09:52.360 particular occasion is taking damage as are quire a few Dodges this time this 216 00:09:52.360 --> 00:09:54.430 well and then he is dead and he's now dead on particular occasion is taking damage as 217 00:09:54.430 --> 00:09:56.829 the last line and again because of the well and then he is dead and he's now dead on 218 00:09:56.829 --> 00:09:58.720 random element the number of Dodges each the last line and again because of the 219 00:09:58.720 --> 00:10:01.240 time will vary greatly but over time it random element the number of Dodges each 220 00:10:01.240 --> 00:10:02.890 should be roughly one-third of each time will vary greatly but over time it 221 00:10:02.890 --> 00:10:05.050 attack that results in a successful should be roughly one-third of each 222 00:10:05.050 --> 00:10:08.709 dodge now note that vlad sometimes starts off attack that results in a successful 223 00:10:08.709 --> 00:10:10.839 with all 12 hit points because you dodge now note that vlad sometimes starts off 224 00:10:10.839 --> 00:10:12.670 sometimes dodge the attack when we with all 12 hit points because you 225 00:10:12.670 --> 00:10:14.380 attempt to take five points of damage sometimes dodge the attack when we 226 00:10:14.380 --> 00:10:16.930 here on line 20 so in other words the attempt to take five points of damage 227 00:10:16.930 --> 00:10:18.610 ability to dodge is now part of the here on line 20 so in other words the 228 00:10:18.610 --> 00:10:21.730 vampire subclass and although ability to dodge is now part of the 229 00:10:21.730 --> 00:10:23.920 vampires inherit from enemy they vampire subclass and although 230 00:10:23.920 --> 00:10:25.930 now behave differently when attacked vampires inherit from enemy they 231 00:10:25.930 --> 00:10:28.899 so you've now seen two of the really now behave differently when attacked 232 00:10:28.899 --> 00:10:30.550 important benefits of using inheritance so you've now seen two of the really 233 00:10:30.550 --> 00:10:34.570 when creating classes first our sub important benefits of using inheritance 234 00:10:34.570 --> 00:10:36.250 classes get all the attributes that's when creating classes first our sub 235 00:10:36.250 --> 00:10:38.709 the data and methods that are defined in classes get all the attributes that's 236 00:10:38.709 --> 00:10:40.600 their base class also known as a the data and methods that are defined in 237 00:10:40.600 --> 00:10:43.060 superclass so that we don't have to their base class also known as a 238 00:10:43.060 --> 00:10:45.339 write the same code again and again for superclass so that we don't have to 239 00:10:45.339 --> 00:10:46.490 each sub class write the same code again and again for 240 00:10:46.490 --> 00:10:50.149 but second we can also extend or alter each sub class 241 00:10:50.149 --> 00:10:52.640 the behavior of a class by creating a but second we can also extend or alter 242 00:10:52.640 --> 00:10:55.820 subclass that adds extra methods or the behavior of a class by creating a 243 00:10:55.820 --> 00:10:57.830 changes the behavior of an existing subclass that adds extra methods or 244 00:10:57.830 --> 00:11:00.200 method we're going to see an example of changes the behavior of an existing 245 00:11:00.200 --> 00:11:02.000 that in the next section when we method we're going to see an example of 246 00:11:02.000 --> 00:11:04.880 subclass the TK inter ist box so that that in the next section when we 247 00:11:04.880 --> 00:11:06.830 it populates itself with data from a subclass the TK inter ist box so that 248 00:11:06.830 --> 00:11:09.709 database now there's one other thing we it populates itself with data from a 249 00:11:09.709 --> 00:11:11.720 might want to do with our classes and database now there's one other thing we 250 00:11:11.720 --> 00:11:13.730 in fact if we were going to do this then might want to do with our classes and 251 00:11:13.730 --> 00:11:15.560 we should have done from the start in fact if we were going to do this then 252 00:11:15.560 --> 00:11:17.120 however i left the two now because if we should have done from the start 253 00:11:17.120 --> 00:11:18.800 you do then you might have a problem however i left the two now because if 254 00:11:18.800 --> 00:11:21.380 with IntelliJ is renaming of variables you do then you might have a problem 255 00:11:21.380 --> 00:11:23.839 so to see what the potential problem is with IntelliJ is renaming of variables 256 00:11:23.839 --> 00:11:27.110 we look at the code here what i'm going to do so to see what the potential problem is 257 00:11:27.110 --> 00:11:29.089 is I'm just going to comment out these we look at the code here what i'm going to do 258 00:11:29.089 --> 00:11:35.089 first two lines and i'm going to put is I'm just going to comment out these 259 00:11:35.089 --> 00:11:37.670 another line of code in here.... first two lines and i'm going to put 260 00:11:37.670 --> 00:11:39.589 ... another line of code in here.... 261 00:11:39.589 --> 00:11:45.500 ... ... 262 00:11:45.500 --> 00:11:49.430 ....you can see here that I've ... 263 00:11:49.430 --> 00:11:51.709 added some code that modified vlad lives ....you can see here that I've 264 00:11:51.709 --> 00:11:54.740 and hit points attributes directly if added some code that modified vlad lives 265 00:11:54.740 --> 00:11:59.630 you run this and surprisingly vlads ends and hit points attributes directly if 266 00:11:59.630 --> 00:12:02.089 up with no lives left and hit points you run this and surprisingly vlads ends 267 00:12:02.089 --> 00:12:04.640 set to only one that's normal behavior up with no lives left and hit points 268 00:12:04.640 --> 00:12:07.070 for python and as we've seen a few times set to only one that's normal behavior 269 00:12:07.070 --> 00:12:09.440 we can't prevent code from accessing the for python and as we've seen a few times 270 00:12:09.440 --> 00:12:12.410 data attributes of our classes what we we can't prevent code from accessing the 271 00:12:12.410 --> 00:12:14.690 can do is providing indication data attributes of our classes what we 272 00:12:14.690 --> 00:12:17.149 that this shouldn't be done and if you can do is providing indication 273 00:12:17.149 --> 00:12:19.520 remember the convention is to prefix the that this shouldn't be done and if you 274 00:12:19.520 --> 00:12:22.520 attribute names with an underscore now remember the convention is to prefix the 275 00:12:22.520 --> 00:12:24.050 ideally we would have done this right from attribute names with an underscore now 276 00:12:24.050 --> 00:12:26.899 the start and in fact of i'd recommend ideally we would have done this right from 277 00:12:26.899 --> 00:12:29.089 prefixing your class data attributes with the start and in fact of i'd recommend 278 00:12:29.089 --> 00:12:31.790 an underscore right away if you decide you prefixing your class data attributes with 279 00:12:31.790 --> 00:12:33.800 want that to you want to hide them and an underscore right away if you decide you 280 00:12:33.800 --> 00:12:35.390 if you do find that you need to access want that to you want to hide them and 281 00:12:35.390 --> 00:12:37.910 them directly you can refactor the ones if you do find that you need to access 282 00:12:37.910 --> 00:12:40.070 that do require direct access or at a them directly you can refactor the ones 283 00:12:40.070 --> 00:12:41.810 property for them as we saw in a that do require direct access or at a 284 00:12:41.810 --> 00:12:43.250 previous video in this section of the property for them as we saw in a 285 00:12:43.250 --> 00:12:46.070 course so the decision on whether to previous video in this section of the 286 00:12:46.070 --> 00:12:48.800 hide the attributes or not isn't course so the decision on whether to 287 00:12:48.800 --> 00:12:50.779 necessarily a simple one and there hide the attributes or not isn't 288 00:12:50.779 --> 00:12:52.490 really are no hard and fast rules that necessarily a simple one and there 289 00:12:52.490 --> 00:12:54.589 apply to all scenarios really are no hard and fast rules that 290 00:12:54.589 --> 00:12:56.270 now keep in mind that the python apply to all scenarios 291 00:12:56.270 --> 00:12:58.190 philosophy is that you probably now keep in mind that the python 292 00:12:58.190 --> 00:12:59.420 shouldn't hide them philosophy is that you probably 293 00:12:59.420 --> 00:13:02.210 unless modifying them directly will break your shouldn't hide them 294 00:13:02.210 --> 00:13:05.060 code somehow so modifying the lives unless modifying them directly will break your 295 00:13:05.060 --> 00:13:06.470 and hit points as we're doing here code somehow so modifying the lives 296 00:13:06.470 --> 00:13:10.430 on line 31 and 32 won't break the code and hit points as we're doing here 297 00:13:10.430 --> 00:13:13.790 so i'd be tempted to leave them as they on line 31 and 32 won't break the code 298 00:13:13.790 --> 00:13:15.860 were but as I want to show you a so i'd be tempted to leave them as they 299 00:13:15.860 --> 00:13:17.750 potential pitfall with renaming were but as I want to show you a 300 00:13:17.750 --> 00:13:19.940 attributes in IntelliJ we'll go ahead potential pitfall with renaming 301 00:13:19.940 --> 00:13:21.920 and mark them as private for one of a attributes in IntelliJ we'll go ahead 302 00:13:21.920 --> 00:13:24.230 better term when I use the term private and mark them as private for one of a 303 00:13:24.230 --> 00:13:25.670 in Python remember i'm talking about better term when I use the term private 304 00:13:25.670 --> 00:13:28.820 intention and things aren't really private in Python remember i'm talking about 305 00:13:28.820 --> 00:13:30.110 alright so we can just go ahead and intention and things aren't really private 306 00:13:30.110 --> 00:13:32.030 refactor our attributes in the enemy alright so we can just go ahead and 307 00:13:32.030 --> 00:13:36.170 class go back to enemy going up here refactor our attributes in the enemy 308 00:13:36.170 --> 00:13:37.370 because it's the enemy class that we class go back to enemy going up here 309 00:13:37.370 --> 00:13:39.350 wanted do our reflecting on going to because it's the enemy class that we 310 00:13:39.350 --> 00:13:41.660 start here with a self.name so wanted do our reflecting on going to 311 00:13:41.660 --> 00:13:44.090 going to right click on name refactor start here with a self.name so 312 00:13:44.090 --> 00:13:48.050 rename just going to add an underscore going to right click on name refactor 313 00:13:48.050 --> 00:13:51.980 to the name press enter so changes rename just going to add an underscore 314 00:13:51.980 --> 00:13:55.250 reflected and going to the same for to the name press enter so changes 315 00:13:55.250 --> 00:13:57.830 hitpoints lives and the life reflected and going to the same for 316 00:13:57.830 --> 00:14:01.010 so each one i'm just going to rename just hitpoints lives and the life 317 00:14:01.010 --> 00:14:09.830 putting _ at the start so each one i'm just going to rename just 318 00:14:09.830 --> 00:14:18.290 alright so that's all 4 now you can putting _ at the start 319 00:14:18.290 --> 00:14:20.750 see here as an example self dot alright so that's all 4 now you can 320 00:14:20.750 --> 00:14:22.940 underscore hit_points equals see here as an example self dot 321 00:14:22.940 --> 00:14:24.890 remaining points as an example of what underscore hit_points equals 322 00:14:24.890 --> 00:14:26.420 intelliJ has done when it's refactored remaining points as an example of what 323 00:14:26.420 --> 00:14:28.790 and renamed things for us so switching intelliJ has done when it's refactored 324 00:14:28.790 --> 00:14:31.910 back to main we can see here that the and renamed things for us so switching 325 00:14:31.910 --> 00:14:33.770 attributes have been renamed as well so back to main we can see here that the 326 00:14:33.770 --> 00:14:35.270 intelliJ done that when we use attributes have been renamed as well so 327 00:14:35.270 --> 00:14:40.880 the refactor but eventually run this we intelliJ done that when we use 328 00:14:40.880 --> 00:14:43.610 actually get an error troll object has the refactor but eventually run this we 329 00:14:43.610 --> 00:14:47.090 no attribute name so intelliJ actually get an error troll object has 330 00:14:47.090 --> 00:14:49.250 normally does a great job but it does no attribute name so intelliJ 331 00:14:49.250 --> 00:14:51.080 fall down on those replacement fields normally does a great job but it does 332 00:14:51.080 --> 00:14:53.330 that we've used before in the enemy fall down on those replacement fields 333 00:14:53.330 --> 00:14:54.410 class in our underscore underscore that we've used before in the enemy 334 00:14:54.410 --> 00:14:59.150 string method this method here and class in our underscore underscore 335 00:14:59.150 --> 00:15:01.070 going back to the error string method this method here and 336 00:15:01.070 --> 00:15:02.450 luckily it's actually telling us its going back to the error 337 00:15:02.450 --> 00:15:04.940 line 27 in the underscore underscore luckily it's actually telling us its 338 00:15:04.940 --> 00:15:07.130 string underscore underscore method so line 27 in the underscore underscore 339 00:15:07.130 --> 00:15:08.900 we have a look at that again that is string underscore underscore method so 340 00:15:08.900 --> 00:15:10.790 this line here so you can see what the we have a look at that again that is 341 00:15:10.790 --> 00:15:12.530 problem is that intelliJ hasn't this line here so you can see what the 342 00:15:12.530 --> 00:15:14.390 actually rename those replacement fields problem is that intelliJ hasn't 343 00:15:14.390 --> 00:15:16.820 correctly now we could just edit up those actually rename those replacement fields 344 00:15:16.820 --> 00:15:19.370 occurrences manually but we've also got correctly now we could just edit up those 345 00:15:19.370 --> 00:15:21.620 to change lines in some other places as occurrences manually but we've also got 346 00:15:21.620 --> 00:15:23.720 well like for example on line 21 and to change lines in some other places as 347 00:15:23.720 --> 00:15:26.330 line 23 they are both referring to the old well like for example on line 21 and 348 00:15:26.330 --> 00:15:27.860 name before it was prefixed with an _ line 23 they are both referring to the old 349 00:15:27.860 --> 00:15:29.780 so it's really a bit of work to name before it was prefixed with an _ 350 00:15:29.780 --> 00:15:32.000 do here and going down looking at the so it's really a bit of work to 351 00:15:32.000 --> 00:15:33.680 troll we've actually got will do here and going down looking at the 352 00:15:33.680 --> 00:15:35.030 need to do some working there as well the troll we've actually got will 353 00:15:35.030 --> 00:15:37.280 troll grunt method hasn't an _ need to do some working there as well the 354 00:15:37.280 --> 00:15:39.980 for the data attributes anymore either troll grunt method hasn't an _ 355 00:15:39.980 --> 00:15:41.660 so quite a bit of work to actually go for the data attributes anymore either 356 00:15:41.660 --> 00:15:43.490 right through and refactor this if we so quite a bit of work to actually go 357 00:15:43.490 --> 00:15:45.740 wanted to do it so it's quite a lot of right through and refactor this if we 358 00:15:45.740 --> 00:15:48.110 work and as I mentioning to do this and wanted to do it so it's quite a lot of 359 00:15:48.110 --> 00:15:50.000 I want to draw your attention to a work and as I mentioning to do this and 360 00:15:50.000 --> 00:15:52.700 possible pitfall with refactoring data I want to draw your attention to a 361 00:15:52.700 --> 00:15:54.770 attributes but i'm going to show you a possible pitfall with refactoring data 362 00:15:54.770 --> 00:15:57.740 quick way to do with that next important attributes but i'm going to show you a 363 00:15:57.740 --> 00:15:59.330 thing here is that you can get problems quick way to do with that next important 364 00:15:59.330 --> 00:16:00.710 if you use this form of replacement thing here is that you can get problems 365 00:16:00.710 --> 00:16:03.170 fields and then try to refactor the if you use this form of replacement 366 00:16:03.170 --> 00:16:05.360 attributes later alright so i'm going to fields and then try to refactor the 367 00:16:05.360 --> 00:16:07.310 end the video here now in the next video attributes later alright so i'm going to 368 00:16:07.310 --> 00:16:08.270 will continue on and we're actually end the video here now in the next video 369 00:16:08.270 --> 00:16:10.310 going to bring some regular expressions will continue on and we're actually 370 00:16:10.310 --> 00:16:12.710 to try and fix this issue so see you in going to bring some regular expressions 371 00:16:12.710 --> 00:16:18.280 the next video to try and fix this issue so see you in