1
00:00:05,450 --> 00:00:08,000
in the previous video we looked at how

2
00:00:08,000 --> 00:00:09,620
to catch exceptions and handle them

3
00:00:09,620 --> 00:00:13,460
using the try and accept statements and

4
00:00:13,460 --> 00:00:15,740
we looked at how to use finally to make

5
00:00:15,740 --> 00:00:17,570
sure that we can run clean up code for

6
00:00:17,570 --> 00:00:20,030
example even if an exception is raised

7
00:00:20,030 --> 00:00:22,390
and then we also saw yet another use of

8
00:00:22,390 --> 00:00:25,430
the else keyword this time to execute

9
00:00:25,430 --> 00:00:28,460
code if an exception isn't raised now

10
00:00:28,460 --> 00:00:31,130
handling exceptions is important to make

11
00:00:31,130 --> 00:00:34,160
sure your code doesn't crash so if you

12
00:00:34,160 --> 00:00:36,290
can take some useful action when an

13
00:00:36,290 --> 00:00:37,790
exception is raised then you should

14
00:00:37,790 --> 00:00:40,250
certainly do so but what happens if you

15
00:00:40,250 --> 00:00:43,340
code can't handle the exception now

16
00:00:43,340 --> 00:00:45,740
if your module relies on a file name that has

17
00:00:45,740 --> 00:00:47,630
been provided by the user for example

18
00:00:47,630 --> 00:00:50,300
they may have entered an invalid volume or

19
00:00:50,300 --> 00:00:53,210
drive letter on Windows now your module

20
00:00:53,210 --> 00:00:55,070
may be called by some other code that

21
00:00:55,070 --> 00:00:57,290
requested the file name so you can't

22
00:00:57,290 --> 00:00:59,000
just go picking a random location on

23
00:00:59,000 --> 00:01:02,020
the computer to write your data to so

24
00:01:02,020 --> 00:01:04,040
in that case there's not a lot that your code

25
00:01:04,040 --> 00:01:06,200
can do about the exception

26
00:01:06,200 --> 00:01:08,270
now you may be tempted to ask the user

27
00:01:08,270 --> 00:01:10,280
for another file name but if you're

28
00:01:10,280 --> 00:01:12,710
code is running on a remote server or as a

29
00:01:12,710 --> 00:01:13,940
windows service

30
00:01:13,940 --> 00:01:16,060
no one will ever see your prompt and

31
00:01:16,060 --> 00:01:17,380
your program will sit there waiting for

32
00:01:17,380 --> 00:01:20,420
input that will never come so in that

33
00:01:20,420 --> 00:01:23,600
case theirs 2 actions you can take one

34
00:01:23,600 --> 00:01:25,700
action really the first thing you can do

35
00:01:25,700 --> 00:01:27,880
is do nothing and we'll see in a minute

36
00:01:27,880 --> 00:01:29,740
that doing nothing can actually be the

37
00:01:29,740 --> 00:01:31,760
best approach and they can be nothing

38
00:01:31,760 --> 00:01:33,980
wrong with ignoring exceptions that you

39
00:01:33,980 --> 00:01:37,240
can't deal with but the other option is

40
00:01:37,240 --> 00:01:39,800
to raise an exception to let the calling

41
00:01:39,800 --> 00:01:42,800
code know that something's gone wrong so

42
00:01:42,800 --> 00:01:44,600
we're going to use our duck class to

43
00:01:44,600 --> 00:01:46,990
demonstrate this so got the game project

44
00:01:46,990 --> 00:01:48,620
open and you can see I've got the duck

45
00:01:48,620 --> 00:01:51,050
stop py file opened so if you haven't

46
00:01:51,050 --> 00:01:52,970
got this file or you've been experimenting

47
00:01:52,970 --> 00:01:54,980
or modifying it then you will find the

48
00:01:54,980 --> 00:01:57,340
original ducks .py file in the

49
00:01:57,340 --> 00:02:00,020
resources section for this video so

50
00:02:00,020 --> 00:02:02,630
we're going to use our duck class in a demo

51
00:02:02,630 --> 00:02:06,200
program to model durk migration now

52
00:02:06,200 --> 00:02:08,240
it's all a bit simplified but if I

53
00:02:08,240 --> 00:02:10,130
waited until we really needed to use

54
00:02:10,130 --> 00:02:12,430
exceptions in our database code you'd

55
00:02:12,430 --> 00:02:14,090
have to learn about them while also

56
00:02:14,090 --> 00:02:16,280
having a lot of new database concepts

57
00:02:16,280 --> 00:02:19,040
to understand so I put this

58
00:02:19,040 --> 00:02:21,170
example together to explain raising

59
00:02:21,170 --> 00:02:23,630
exceptions before we need to do things

60
00:02:23,630 --> 00:02:26,680
like this in real code now our duck

61
00:02:26,680 --> 00:02:29,320
class maybe we'll be very simple but

62
00:02:29,320 --> 00:02:31,400
what we're about to do is exactly what you

63
00:02:31,400 --> 00:02:34,250
would do in real modules to make them

64
00:02:34,250 --> 00:02:36,400
more robust and to make debugging

65
00:02:36,400 --> 00:02:37,150
easier

66
00:02:37,150 --> 00:02:39,820
ok so should either have the game project

67
00:02:39,820 --> 00:02:42,350
loaded which I've got on screen there or

68
00:02:42,350 --> 00:02:43,730
you should have created a new python

69
00:02:43,730 --> 00:02:46,310
project with a Python file called ducks

70
00:02:46,310 --> 00:02:48,560
and then paste in the text from ducks.py

71
00:02:48,560 --> 00:02:50,900
from the resources section of this

72
00:02:50,900 --> 00:02:53,770
video so what I'm going to do is

73
00:02:53,770 --> 00:02:56,420
start by adding a new class to duck.py

74
00:02:56,420 --> 00:02:59,810
to represent a flock of ducks i'm

75
00:02:59,810 --> 00:03:01,730
going to call this class flock and add it

76
00:03:01,730 --> 00:03:03,490
after the penguin class down here at

77
00:03:03,490 --> 00:03:05,960
the bottom going to clean up some of

78
00:03:05,960 --> 00:03:11,810
this code that's been commented out so

79
00:03:11,810 --> 00:03:13,100
I'm actually going to add this

80
00:03:13,100 --> 00:03:15,680
class here on line 45 actually what I

81
00:03:15,680 --> 00:03:17,420
might do just make it a space down here so

82
00:03:17,420 --> 00:03:18,620
it's a little bit easier for us to

83
00:03:18,620 --> 00:03:22,490
see going to start with class...

84
00:03:22,490 --> 00:03:29,680
....

85
00:03:29,680 --> 00:04:13,990
....

86
00:04:13,990 --> 00:04:19,070
ok so there's our flock class so it's a

87
00:04:19,070 --> 00:04:21,890
fairly basic classes you can see when we

88
00:04:21,890 --> 00:04:25,150
create a new flock the unique method in

89
00:04:25,150 --> 00:04:27,890
line 48 creates an empty list called

90
00:04:27,890 --> 00:04:30,080
flock and that's with a lowercase as you

91
00:04:30,080 --> 00:04:32,390
can see there now we got an add

92
00:04:32,390 --> 00:04:35,060
duck method to add new ducks to the

93
00:04:35,060 --> 00:04:37,970
flock and also this migrate method that

94
00:04:37,970 --> 00:04:40,400
causes every duck in the flock to fly by

95
00:04:40,400 --> 00:04:43,340
calling their fly method so next what

96
00:04:43,340 --> 00:04:45,680
we're going to do is use our duck

97
00:04:45,680 --> 00:04:47,840
objects in another program so we're

98
00:04:47,840 --> 00:04:49,190
going to create a new python file

99
00:04:49,190 --> 00:04:59,450
called migration migration so we need to

100
00:04:59,450 --> 00:05:01,880
import ducks so that we can use the

101
00:05:01,880 --> 00:05:03,950
object from ducks.py and then i'm going

102
00:05:03,950 --> 00:05:05,570
to create a new flock and add some ducks

103
00:05:05,570 --> 00:05:11,330
to that so import...

104
00:05:11,330 --> 00:05:16,850
....

105
00:05:16,850 --> 00:05:37,760
....

106
00:05:37,760 --> 00:05:46,460
...okay so

107
00:05:46,460 --> 00:05:47,930
obviously I didn't try to think up other

108
00:05:47,930 --> 00:05:50,110
names so for each duck but if you're in

109
00:05:50,110 --> 00:05:52,430
a habit of naming duck then

110
00:05:52,430 --> 00:05:53,750
you've either got a good scientific

111
00:05:53,750 --> 00:05:55,970
reason for doing so or a very strange

112
00:05:55,970 --> 00:05:56,510
hobby

113
00:05:56,510 --> 00:05:58,610
alright so we've now got a new flock

114
00:05:58,610 --> 00:05:59,600
object

115
00:05:59,600 --> 00:06:01,850
we've also got the seven ducks that

116
00:06:01,850 --> 00:06:03,610
we've defined herein line 4 through

117
00:06:03,610 --> 00:06:04,310
10

118
00:06:04,310 --> 00:06:07,610
so let's now go ahead and add the Ducks

119
00:06:07,610 --> 00:06:14,780
to the flock....

120
00:06:14,780 --> 00:06:17,560
...

121
00:06:17,560 --> 00:06:20,790
....

122
00:06:20,790 --> 00:06:52,370
....

123
00:06:52,370 --> 00:06:54,890
....so

124
00:06:54,890 --> 00:06:56,690
it now contains seven ducks and we

125
00:06:56,690 --> 00:06:58,700
should be able to make the migrate so

126
00:06:58,700 --> 00:07:02,720
we do that by typing...

127
00:07:02,720 --> 00:07:04,580
so try running that to confirm that it

128
00:07:04,580 --> 00:07:08,210
works you can see that time we've got

129
00:07:08,210 --> 00:07:11,450
seven messages their so each duck is

130
00:07:11,450 --> 00:07:14,450
successfully flying so far that's

131
00:07:14,450 --> 00:07:17,270
working fine but the problem comes if i

132
00:07:17,270 --> 00:07:19,760
try to add percy the penguin to the

133
00:07:19,760 --> 00:07:21,560
flock let me show you what i mean so i'm

134
00:07:21,560 --> 00:07:26,210
going to do....

135
00:07:26,210 --> 00:07:30,290
...

136
00:07:30,290 --> 00:07:34,010
...

137
00:07:34,010 --> 00:07:37,670
....

138
00:07:37,670 --> 00:07:43,700
....you see that we've

139
00:07:43,700 --> 00:07:46,550
actually got an error so as you can see

140
00:07:46,550 --> 00:07:48,110
here we've only got four of our ducks

141
00:07:48,110 --> 00:07:50,300
that have managed to fly the other three

142
00:07:50,300 --> 00:07:53,120
grounded and failed to take off and again

143
00:07:53,120 --> 00:07:54,620
percy he had a problem trying to fly as

144
00:07:54,620 --> 00:07:57,200
well and i put this into

145
00:07:57,200 --> 00:07:59,840
floating mode right now to make it easier

146
00:07:59,840 --> 00:08:03,200
and probably see those links a little

147
00:08:03,200 --> 00:08:06,230
easier so you probably recall from we're

148
00:08:06,230 --> 00:08:08,270
setting up a penguin that this was going

149
00:08:08,270 --> 00:08:09,890
to happen and that's because penguins

150
00:08:09,890 --> 00:08:12,530
can't fly so the migrate method crashes

151
00:08:12,530 --> 00:08:14,540
when it tries to call Percy's fly method

152
00:08:14,540 --> 00:08:17,000
so we're going to use this simple

153
00:08:17,000 --> 00:08:19,160
example to look at how we can handle

154
00:08:19,160 --> 00:08:21,950
problems like this and the first thing we

155
00:08:21,950 --> 00:08:23,720
should do is to provide the users of our

156
00:08:23,720 --> 00:08:27,050
modules with a hint as to what they

157
00:08:27,050 --> 00:08:29,600
should be passing to our methods now

158
00:08:29,600 --> 00:08:33,289
python 3.5 added support for type hints

159
00:08:33,289 --> 00:08:36,350
in function and method declarations but

160
00:08:36,350 --> 00:08:38,720
function annotations have been around

161
00:08:38,720 --> 00:08:41,900
since python 3.0 you can use an

162
00:08:41,900 --> 00:08:44,059
annotation in exactly the same format as

163
00:08:44,059 --> 00:08:46,220
as a type in what we're about to do will

164
00:08:46,220 --> 00:08:49,130
work with all versions of python 3 in

165
00:08:49,130 --> 00:08:51,710
fact type hints it's just a formalization of

166
00:08:51,710 --> 00:08:54,590
using function annotations for static type

167
00:08:54,590 --> 00:08:57,440
analysis and it was mainly done so that

168
00:08:57,440 --> 00:08:59,840
type checking tools have a standard to

169
00:08:59,840 --> 00:09:01,940
work with now the lint checker in

170
00:09:01,940 --> 00:09:04,010
IntelliJ that gives us those warnings

171
00:09:04,010 --> 00:09:05,750
and errors in the right margin

172
00:09:05,750 --> 00:09:08,590
is one such checker alright so let's add

173
00:09:08,590 --> 00:09:11,210
some hints to our add duck method so that

174
00:09:11,210 --> 00:09:13,700
at least anyone using attacks module has

175
00:09:13,700 --> 00:09:15,800
an indication of what they should be

176
00:09:15,800 --> 00:09:18,710
providing to the method so we're going

177
00:09:18,710 --> 00:09:21,800
to actually just come back here close down

178
00:09:21,800 --> 00:09:24,650
run window and i'm going to add

179
00:09:24,650 --> 00:09:28,310
the code back in ducks.py and

180
00:09:28,310 --> 00:09:29,300
specifically what we're going to be

181
00:09:29,300 --> 00:09:32,870
looking at is the flock object and we're

182
00:09:32,870 --> 00:09:34,490
going to add hint to the add duck method

183
00:09:34,490 --> 00:09:38,210
so parameters are annotated by using a

184
00:09:38,210 --> 00:09:40,580
colon followed by the type of the

185
00:09:40,580 --> 00:09:42,710
parameter and return values are

186
00:09:42,710 --> 00:09:45,220
indicated using the literal a dash and a

187
00:09:45,220 --> 00:09:46,460
greater than symbol

188
00:09:46,460 --> 00:09:48,680
let's go ahead and do that so with duck

189
00:09:48,680 --> 00:09:53,120
we're going to put...

190
00:09:53,120 --> 00:09:56,060
...

191
00:09:56,060 --> 00:10:08,020
...

192
00:10:08,020 --> 00:10:10,780
...so again parameters

193
00:10:10,780 --> 00:10:12,680
are annotated by using a colon as you

194
00:10:12,680 --> 00:10:15,650
can see here and then now followed by

195
00:10:15,650 --> 00:10:17,750
the type of the perimeter and the return

196
00:10:17,750 --> 00:10:19,460
value can see I use a dash and a

197
00:10:19,460 --> 00:10:22,640
greater than sign and then the type of

198
00:10:22,640 --> 00:10:24,340
the parameter in this case we're using

199
00:10:24,340 --> 00:10:26,690
none when I switch back now that I've

200
00:10:26,690 --> 00:10:29,750
done that to migration py so you can

201
00:10:29,750 --> 00:10:31,370
see that intelliJ lint checker has

202
00:10:31,370 --> 00:10:33,380
picked up the annotation and we've

203
00:10:33,380 --> 00:10:34,900
actually got this percy showing a

204
00:10:34,900 --> 00:10:36,890
warning if I can hover over that now

205
00:10:36,890 --> 00:10:42,020
expected type duck got penguin instead if

206
00:10:42,020 --> 00:10:44,210
you click on one of the add duck method calls

207
00:10:44,210 --> 00:10:46,640
doesn't matter which one then use

208
00:10:46,640 --> 00:10:50,720
control q or ctrl J on a Mac there's now

209
00:10:50,720 --> 00:10:52,250
indication that we should be adding

210
00:10:52,250 --> 00:10:54,110
objects of type duck as you can see there

211
00:10:54,110 --> 00:10:57,110
now note that self has been annotated

212
00:10:57,110 --> 00:11:00,800
automatically self flock up here there's

213
00:11:00,800 --> 00:11:03,460
no need to annotate the self parameter

214
00:11:03,460 --> 00:11:07,460
in method calls so function annotations

215
00:11:07,460 --> 00:11:09,920
or type hints as they're called they

216
00:11:09,920 --> 00:11:12,520
do use exactly the same mechanism can be

217
00:11:12,520 --> 00:11:15,200
very useful now they're very simple to

218
00:11:15,200 --> 00:11:16,780
use so there's only one other thing i

219
00:11:16,780 --> 00:11:19,010
want to say about them now if you use a

220
00:11:19,010 --> 00:11:19,400
hint

221
00:11:19,400 --> 00:11:21,470
for one of the parameters then add a

222
00:11:21,470 --> 00:11:23,390
hint for all of them including the

223
00:11:23,390 --> 00:11:25,610
return value you can find more

224
00:11:25,610 --> 00:11:28,880
information of this in pet 484 lets

225
00:11:28,880 --> 00:11:30,500
just to open a browser I will quickly show

226
00:11:30,500 --> 00:11:30,750
that

227
00:11:30,750 --> 00:11:38,030
[Music]

228
00:11:38,030 --> 00:11:41,380
if you scroll down here

229
00:11:41,380 --> 00:11:44,830
here under the non goals section it

230
00:11:44,830 --> 00:11:46,900
should be emphasized that python will

231
00:11:46,900 --> 00:11:49,630
remain a dynamically typed language and

232
00:11:49,630 --> 00:11:51,430
the authors have no desire to ever make

233
00:11:51,430 --> 00:11:54,250
type hints mandatory even by convention

234
00:11:54,250 --> 00:11:57,640
and the second parameter under here the

235
00:11:57,640 --> 00:11:59,320
next section the meaning of annotations

236
00:11:59,320 --> 00:12:02,340
that recommends that you add hints for

237
00:12:02,340 --> 00:12:05,170
all arguments and the return type if

238
00:12:05,170 --> 00:12:06,670
you're going to add hints for any of them

239
00:12:06,670 --> 00:12:08,320
and I think that's probably good

240
00:12:08,320 --> 00:12:09,640
practice so do that

241
00:12:09,640 --> 00:12:11,860
ok so we've given the users of our

242
00:12:11,860 --> 00:12:14,340
module an indication of what they should

243
00:12:14,340 --> 00:12:16,180
pass to the add_duck_

244
00:12:16,180 --> 00:12:19,590
method as if the name was an

245
00:12:19,590 --> 00:12:22,270
indication enough in this case but that

246
00:12:22,270 --> 00:12:24,130
still doesn't prevent it from crashing

247
00:12:24,130 --> 00:12:26,860
if they ignore the hint so if we go ahead

248
00:12:26,860 --> 00:12:30,880
and run this migration .py we still

249
00:12:30,880 --> 00:12:31,810
get crashing

250
00:12:31,810 --> 00:12:34,720
so let's stop the video here and in the

251
00:12:34,720 --> 00:12:35,920
next video we're going to have a look at

252
00:12:35,920 --> 00:12:38,260
this stack trace and talk about this a

253
00:12:38,260 --> 00:12:39,940
little bit further so we'll see you in

254
00:12:39,940 --> 00:12:45,470
the next video

