1
00:00:04,880 --> 00:00:07,530
so at the end of the last video we

2
00:00:07,530 --> 00:00:09,900
created troll class which if you recall

3
00:00:09,900 --> 00:00:13,370
was a subclass of enemy at the moment

4
00:00:13,370 --> 00:00:15,320
it's not doing anything useful as you

5
00:00:15,320 --> 00:00:17,210
saw we could just as easily use an enemy

6
00:00:17,210 --> 00:00:19,730
instead and get exactly the same result

7
00:00:19,730 --> 00:00:22,710
so inspired by his brothers fine example

8
00:00:22,710 --> 00:00:25,710
ugs game or has been going to the gym and

9
00:00:25,710 --> 00:00:28,140
working out in fact he's still a bit of

10
00:00:28,140 --> 00:00:30,330
a trend and now all the trolls have

11
00:00:30,330 --> 00:00:32,970
been toughened up so every trolls that

12
00:00:32,970 --> 00:00:34,980
our adventurers encounter in this game

13
00:00:34,980 --> 00:00:39,000
will now take 23 points of damage now

14
00:00:39,000 --> 00:00:40,710
because they're mortal only have one

15
00:00:40,710 --> 00:00:42,890
life so there's no need to specify

16
00:00:42,890 --> 00:00:45,840
either value when we create them what we

17
00:00:45,840 --> 00:00:47,910
need to do is initialize the troll

18
00:00:47,910 --> 00:00:50,300
instances with just a name so that we

19
00:00:50,300 --> 00:00:52,670
can tell them apart the hit points and

20
00:00:52,670 --> 00:00:54,570
lives will be the same for every troll

21
00:00:54,570 --> 00:00:57,890
that we create so going back to our

22
00:00:57,890 --> 00:01:00,770
enemy class where our trolls are defined

23
00:01:00,770 --> 00:01:03,960
at the moment we just got pass in their so

24
00:01:03,960 --> 00:01:04,910
we're going to change that we're going

25
00:01:04,910 --> 00:01:07,550
to actually delete that and then we're

26
00:01:07,550 --> 00:01:09,570
going to come down here we're going to

27
00:01:09,570 --> 00:01:13,500
add our init....

28
00:01:13,500 --> 00:01:15,960
....

29
00:01:15,960 --> 00:01:18,570
....

30
00:01:18,570 --> 00:01:22,920
...

31
00:01:22,920 --> 00:01:26,520
...

32
00:01:26,520 --> 00:01:27,960
so the hit points and lives will be the same

33
00:01:27,960 --> 00:01:30,360
for every troll we create the name the

34
00:01:30,360 --> 00:01:31,830
instance rather is just going to have a

35
00:01:31,830 --> 00:01:33,960
name and that's why we're passing it as

36
00:01:33,960 --> 00:01:35,790
a parameter to the constructor to the

37
00:01:35,790 --> 00:01:37,890
init method the underscore underscore init

38
00:01:37,890 --> 00:01:38,460
init method the underscore underscore init

39
00:01:38,460 --> 00:01:40,410
__ which all here

40
00:01:40,410 --> 00:01:41,760
refer to as init

41
00:01:41,760 --> 00:01:44,280
alright so let's try running this and

42
00:01:44,280 --> 00:01:45,240
see what happens

43
00:01:45,240 --> 00:01:50,700
alright so the error we're getting here

44
00:01:50,700 --> 00:01:53,430
on line 3 is missing one required

45
00:01:53,430 --> 00:01:55,050
positional arguments name

46
00:01:55,050 --> 00:01:57,810
we've got an error running this code and

47
00:01:57,810 --> 00:01:59,100
if you think about it the reason is

48
00:01:59,100 --> 00:02:00,150
quite clear

49
00:02:00,150 --> 00:02:01,830
that's because we need to pass an

50
00:02:01,830 --> 00:02:05,430
argument to the troll constructor when

51
00:02:05,430 --> 00:02:07,590
we're creating it so i'm going to do is

52
00:02:07,590 --> 00:02:10,259
come up here to line 3 and change this

53
00:02:10,259 --> 00:02:12,710
so it's got troll and we're going to

54
00:02:12,710 --> 00:02:16,500
put the name in there pug so let's run

55
00:02:16,500 --> 00:02:17,260
that

56
00:02:17,260 --> 00:02:20,790
this time we've actually got an error on

57
00:02:20,790 --> 00:02:22,930
line four and we got one below as well

58
00:02:22,930 --> 00:02:25,000
which we won't worry about for now but

59
00:02:25,000 --> 00:02:28,900
the call to print on line we are relying on

60
00:02:28,900 --> 00:02:30,580
the string method that we added to the

61
00:02:30,580 --> 00:02:33,010
enemy class that's actually getting an

62
00:02:33,010 --> 00:02:35,010
error here on line 4 and also down

63
00:02:35,010 --> 00:02:38,680
here on line 18 in the enemy class when

64
00:02:38,680 --> 00:02:39,730
we tried and that's what we try to

65
00:02:39,730 --> 00:02:41,830
access the attributes of our troll

66
00:02:41,830 --> 00:02:44,620
instance now by the way when reading

67
00:02:44,620 --> 00:02:46,240
these stack traces that python

68
00:02:46,240 --> 00:02:48,250
produces i actually like to read them

69
00:02:48,250 --> 00:02:49,300
from the bottom up

70
00:02:49,300 --> 00:02:51,240
I think they make more sense that way so

71
00:02:51,240 --> 00:02:53,350
starting at the bottom here our

72
00:02:53,350 --> 00:02:55,540
code raise an attribute

73
00:02:55,540 --> 00:02:58,390
error because our troll object has no

74
00:02:58,390 --> 00:03:01,540
attribute name reading upwards the error

75
00:03:01,540 --> 00:03:05,170
happened at line 18 in enemypy

76
00:03:05,170 --> 00:03:07,650
in the string method and that's

77
00:03:07,650 --> 00:03:09,010
where we were trying to access the

78
00:03:09,010 --> 00:03:11,430
attributes to format them into the

79
00:03:11,430 --> 00:03:14,620
string that we want to print and that

80
00:03:14,620 --> 00:03:17,140
was called right here on line 4 in main

81
00:03:17,140 --> 00:03:18,580
which is why we've got this other error

82
00:03:18,580 --> 00:03:20,560
showing that's obviously the

83
00:03:20,560 --> 00:03:22,570
print statement so what's actually

84
00:03:22,570 --> 00:03:25,870
happened here so why is our troll lost all

85
00:03:25,870 --> 00:03:27,610
the attributes that it just had in the

86
00:03:27,610 --> 00:03:30,210
previous video well the reason is we

87
00:03:30,210 --> 00:03:32,490
added the init method to the troll class

88
00:03:32,490 --> 00:03:35,050
and when we did that Python longer points the

89
00:03:35,050 --> 00:03:38,170
enemy classes and init method so it runs the

90
00:03:38,170 --> 00:03:41,140
init method in our troll class instead so

91
00:03:41,140 --> 00:03:43,450
if the enemy's init method doesn't run

92
00:03:43,450 --> 00:03:46,540
then the attributes don't gets set

93
00:03:46,540 --> 00:03:48,930
so what we actually need to do here is

94
00:03:48,930 --> 00:03:50,740
call the init method of the superclass

95
00:03:50,740 --> 00:03:55,260
enemy inside our trolls init method now

96
00:03:55,260 --> 00:03:56,920
there's a few ways to do this and i'll

97
00:03:56,920 --> 00:03:59,520
leave the one I recommend in python 3 to

98
00:03:59,520 --> 00:04:02,460
last so we'll go back to our enemy class

99
00:04:02,460 --> 00:04:06,100
and down here to troll now firstly we

100
00:04:06,100 --> 00:04:08,290
can actually prefix the call to init

101
00:04:08,290 --> 00:04:11,350
with the name of the superclass so we

102
00:04:11,350 --> 00:04:12,880
can do something like get rid of the

103
00:04:12,880 --> 00:04:16,120
pass...

104
00:04:16,120 --> 00:04:18,990
....

105
00:04:18,990 --> 00:04:19,570
...

106
00:04:19,570 --> 00:04:30,070
...

107
00:04:30,070 --> 00:04:33,820
but that would still get an error
...

108
00:04:33,820 --> 00:04:35,920
because we've got to these other lines

109
00:04:35,920 --> 00:04:39,400
here on lines six and nine got

110
00:04:39,400 --> 00:04:41,110
multiple parameters we're passing the

111
00:04:41,110 --> 00:04:43,180
hit points or both the hit points and the

112
00:04:43,180 --> 00:04:46,000
number of lives so let's actually fix

113
00:04:46,000 --> 00:04:48,370
those first so their just the names

114
00:04:48,370 --> 00:04:50,440
they're just one parameter just to see

115
00:04:50,440 --> 00:04:56,200
what happens if you run this now the

116
00:04:56,200 --> 00:04:57,970
program runs and all our trolls you

117
00:04:57,970 --> 00:05:00,520
can see now have 23 hitpoints well

118
00:05:00,520 --> 00:05:01,750
actually going to say the all do but

119
00:05:01,750 --> 00:05:04,180
urgh doesn't that's because we're using the

120
00:05:04,180 --> 00:05:07,000
enemy class here instead of troll but if I change

121
00:05:07,000 --> 00:05:11,830
that to troll run that again we see now

122
00:05:11,830 --> 00:05:14,680
that they all do have one life and 23 hit points

123
00:05:14,680 --> 00:05:16,860
so if they all have different

124
00:05:16,860 --> 00:05:19,240
names so that we can tell them apart

125
00:05:19,240 --> 00:05:21,360
although telling one troll from another

126
00:05:21,360 --> 00:05:23,410
is a specialized skill it won't be

127
00:05:23,410 --> 00:05:24,820
covering that in the course

128
00:05:24,820 --> 00:05:26,740
alright so let's go back to the trolll

129
00:05:26,740 --> 00:05:30,100
class enemy.py and lets have a look at

130
00:05:30,100 --> 00:05:32,800
what we did here so we created an init

131
00:05:32,800 --> 00:05:35,230
method for our troll class and when we did

132
00:05:35,230 --> 00:05:37,660
that the init method of enemy is no

133
00:05:37,660 --> 00:05:40,110
longer called you could say that the

134
00:05:40,110 --> 00:05:42,550
trolls init method had shadowed init

135
00:05:42,550 --> 00:05:45,580
method in its superclass so to get a

136
00:05:45,580 --> 00:05:47,610
around that and make sure that class

137
00:05:47,610 --> 00:05:50,440
attributes are initialized we called the

138
00:05:50,440 --> 00:05:53,260
enemy superclass init method that's the

139
00:05:53,260 --> 00:05:59,470
call on line 24 by prefixing the method name with the class name now its imporant

140
00:05:59,470 --> 00:06:00,910
that we provide the correct number of

141
00:06:00,910 --> 00:06:03,760
parameters but because the enemy init

142
00:06:03,760 --> 00:06:05,740
method has default values we could have

143
00:06:05,740 --> 00:06:07,210
left off one or more of the parameters

144
00:06:07,210 --> 00:06:09,400
out here this ones here

145
00:06:09,400 --> 00:06:11,440
you won't always want to provide default

146
00:06:11,440 --> 00:06:13,390
values in your class methods so it's

147
00:06:13,390 --> 00:06:14,710
important to remember that we are

148
00:06:14,710 --> 00:06:16,390
calling the superclass init method

149
00:06:16,390 --> 00:06:18,730
here and it needs 4 argument's

150
00:06:18,730 --> 00:06:21,820
now our trolls init method only takes two

151
00:06:21,820 --> 00:06:23,890
arguments so we should provide values

152
00:06:23,890 --> 00:06:26,140
for the remaining two alright so that's

153
00:06:26,140 --> 00:06:27,820
one way to do it and that would be the

154
00:06:27,820 --> 00:06:31,360
only way available in python 2 now in

155
00:06:31,360 --> 00:06:33,970
python three we can use the super

156
00:06:33,970 --> 00:06:36,280
method instead and if you want to cope

157
00:06:36,280 --> 00:06:38,650
with multiple inheritance you must use

158
00:06:38,650 --> 00:06:39,660
super

159
00:06:39,660 --> 00:06:41,160
instead of specifying the base class

160
00:06:41,160 --> 00:06:43,680
name so let's do that so i'm going to

161
00:06:43,680 --> 00:06:45,870
change this enemy part here delete that

162
00:06:45,870 --> 00:06:49,500
out we're going to put...

163
00:06:49,500 --> 00:06:57,000
...

164
00:06:57,000 --> 00:07:00,210
...

165
00:07:00,210 --> 00:07:03,750
....let's just run that to make sure it

166
00:07:03,750 --> 00:07:10,810
still works it still works now syntax

167
00:07:10,810 --> 00:07:12,720
does make it a little bit hard to read

168
00:07:12,720 --> 00:07:15,270
but what we're basically doing is passing the

169
00:07:15,270 --> 00:07:18,000
name of our class troll and the current

170
00:07:18,000 --> 00:07:21,030
instance self to super and then we're

171
00:07:21,030 --> 00:07:23,400
using that to call the init method of

172
00:07:23,400 --> 00:07:25,710
the base class now when you're using

173
00:07:25,710 --> 00:07:27,990
super inside of class instance there's a

174
00:07:27,990 --> 00:07:30,330
more readable shorthand way of writing

175
00:07:30,330 --> 00:07:33,630
unfortunately and we can do away with

176
00:07:33,630 --> 00:07:36,360
this troll and self in the parentheses

177
00:07:36,360 --> 00:07:39,600
just change it so it's got super with

178
00:07:39,600 --> 00:07:41,310
the parentheses dot than the __

179
00:07:41,310 --> 00:07:43,950
init and that would be the

180
00:07:43,950 --> 00:07:45,720
way that I suggest you normally use it

181
00:07:45,720 --> 00:07:47,610
when calling methods in the superclass

182
00:07:47,610 --> 00:07:50,280
and it does it the identical thing to

183
00:07:50,280 --> 00:07:51,900
what we had previously and I will just undo

184
00:07:51,900 --> 00:07:58,680
that take a copy of that line and cut

185
00:07:58,680 --> 00:08:00,450
that out

186
00:08:00,450 --> 00:08:04,590
fix it so it's doing the same thing as

187
00:08:04,590 --> 00:08:06,570
the commented out line on line 24

188
00:08:06,570 --> 00:08:09,720
but the compiler knows the type troll

189
00:08:09,720 --> 00:08:12,030
and the instant self so there's really

190
00:08:12,030 --> 00:08:13,920
no need to specify them in the super

191
00:08:13,920 --> 00:08:14,560
call

192
00:08:14,560 --> 00:08:18,090
ok so now our trolls behave differently to a

193
00:08:18,090 --> 00:08:20,790
basic enemy when we create a new troll

194
00:08:20,790 --> 00:08:23,310
we just give him or her a name and hit

195
00:08:23,310 --> 00:08:25,860
points are live are automatically set for

196
00:08:25,860 --> 00:08:29,130
us now subclasses can have additional

197
00:08:29,130 --> 00:08:32,220
attributes to extend their behavior and

198
00:08:32,220 --> 00:08:33,990
allow them to do things that their base

199
00:08:33,990 --> 00:08:36,960
class doesn't do so lets demonstrate

200
00:08:36,960 --> 00:08:40,230
that by giving our trolls a grunt method

201
00:08:40,230 --> 00:08:43,289
will come down here

202
00:08:43,289 --> 00:08:49,590
and do...

203
00:08:49,590 --> 00:08:56,170
....

204
00:08:56,170 --> 00:09:06,210
...

205
00:09:06,210 --> 00:09:09,430
alright so that's the grunt method added

206
00:09:09,430 --> 00:09:11,620
to the troll class so let's get our

207
00:09:11,620 --> 00:09:12,960
trolls to grunt by adding a bit more

208
00:09:12,960 --> 00:09:16,710
code in the main py file....

209
00:09:16,710 --> 00:09:20,080
...

210
00:09:20,080 --> 00:09:27,880
....

211
00:09:27,880 --> 00:09:30,190
...

212
00:09:30,190 --> 00:09:37,590
...

213
00:09:37,590 --> 00:09:42,270
....

214
00:09:42,270 --> 00:09:45,330
....the

215
00:09:45,330 --> 00:09:46,870
troubles with trolls is that once you set

216
00:09:46,870 --> 00:09:48,760
one off that you'll have to join in so

217
00:09:48,760 --> 00:09:50,130
let's go ahead and actually add the code

218
00:09:50,130 --> 00:09:53,200
for the others so we'll do another troll....

219
00:09:53,200 --> 00:09:56,920
....

220
00:09:56,920 --> 00:10:03,720
....and then run that me

221
00:10:03,720 --> 00:10:06,030
pug pug stop stomping me ugh ughs

222
00:10:06,030 --> 00:10:09,210
stomp you stomp you then me urg urgh

223
00:10:09,210 --> 00:10:11,800
so about now a wise adventure might be

224
00:10:11,800 --> 00:10:14,170
decided that it's time to leave so

225
00:10:14,170 --> 00:10:16,830
getting back to our troll sub class

226
00:10:16,830 --> 00:10:19,080
it can now do things that a basic

227
00:10:19,080 --> 00:10:21,190
enemy cant and if we try and get an

228
00:10:21,190 --> 00:10:23,110
enemy instance to grant them then will get an

229
00:10:23,110 --> 00:10:25,080
error so just to demonstrate that

230
00:10:25,080 --> 00:10:32,340
....

231
00:10:32,340 --> 00:10:37,020
...

232
00:10:37,020 --> 00:10:40,200
...

233
00:10:40,200 --> 00:10:44,080
...and if you try to run that not

234
00:10:44,080 --> 00:10:46,630
surprising we get an error enemy object

235
00:10:46,630 --> 00:10:49,990
has no attribute grunt we just highlight

236
00:10:49,990 --> 00:10:51,370
that with the mouse unresolved

237
00:10:51,370 --> 00:10:53,250
attribute reference grunt for class enemy

238
00:10:53,250 --> 00:10:55,870
which again isn't surprising because it

239
00:10:55,870 --> 00:10:56,360
hasn't

240
00:10:56,360 --> 00:10:58,430
the enemy class hasn't got a grunt

241
00:10:58,430 --> 00:10:59,470
method

242
00:10:59,470 --> 00:11:01,490
ok so as well as adding extra behavior

243
00:11:01,490 --> 00:11:04,190
to our subclasses we can now also change

244
00:11:04,190 --> 00:11:06,700
the behavior of methods that exists in

245
00:11:06,700 --> 00:11:09,620
the superclass to see how to do that

246
00:11:09,620 --> 00:11:11,770
we're going to start off by creating a

247
00:11:11,770 --> 00:11:14,360
new class but let's start working on

248
00:11:14,360 --> 00:11:20,970
that in the next video

