1
00:00:05,260 --> 00:00:06,550
All right, so as I mentioned

2
00:00:06,550 --> 00:00:08,340
towards the end of the last video,

3
00:00:08,340 --> 00:00:10,470
there's a slight problem with this code

4
00:00:10,470 --> 00:00:13,120
and I'm gonna get you to look at a challenge

5
00:00:13,120 --> 00:00:14,950
of fixing that shortly,

6
00:00:14,950 --> 00:00:18,610
but before we do that, let's see what the actual problem is.

7
00:00:18,610 --> 00:00:20,950
So what happens if the calling code specifies

8
00:00:20,950 --> 00:00:24,910
a key word argument that our function is already using?

9
00:00:24,910 --> 00:00:27,990
So in our code, we need to suppress the end of line

10
00:00:27,990 --> 00:00:31,450
character when we call the built in print function.

11
00:00:31,450 --> 00:00:33,070
So we're parsing, as you can see here,

12
00:00:33,070 --> 00:00:36,400
the end equals on line four with the little space there

13
00:00:36,400 --> 00:00:38,660
and I'm going to actually show you,

14
00:00:38,660 --> 00:00:41,170
I'm gonna change the calling code on line seven

15
00:00:41,170 --> 00:00:44,080
to put to also specify the end argument,

16
00:00:44,080 --> 00:00:46,220
which would be something that ordinarily

17
00:00:46,220 --> 00:00:48,390
would be a perfectly valid thing to do.

18
00:00:48,390 --> 00:00:51,440
So if I change that and put end=

19
00:00:51,440 --> 00:00:54,350
single quotes, a new line character.

20
00:00:54,350 --> 00:00:56,930
So if we actually run this now.

21
00:00:59,520 --> 00:01:01,040
So what's happening here is you can see

22
00:01:01,040 --> 00:01:04,349
there's an error is that kwargs contains the end argument.

23
00:01:04,349 --> 00:01:07,410
You can see that I'm calling that there.

24
00:01:07,410 --> 00:01:10,630
You can see that's showing in the printout of kwargs

25
00:01:10,630 --> 00:01:13,710
that we're doing on line two in the console output.

26
00:01:13,710 --> 00:01:15,850
But then we're also providing a value for it

27
00:01:15,850 --> 00:01:17,440
here on line four.

28
00:01:17,440 --> 00:01:20,350
You can't actually specify the same key word twice,

29
00:01:20,350 --> 00:01:22,050
and that's the problem.

30
00:01:22,050 --> 00:01:24,850
The specific error, got multiple valleys

31
00:01:24,850 --> 00:01:26,900
for key word argument end.

32
00:01:26,900 --> 00:01:29,180
And consequently we're getting a crash for that.

33
00:01:29,180 --> 00:01:31,140
Alright, so there's two ways to deal with this

34
00:01:31,140 --> 00:01:35,320
and I'm gonna show both in your solution to the challenge.

35
00:01:35,320 --> 00:01:37,820
Okay, but onto your challenge.

36
00:01:40,420 --> 00:01:44,320
You need to look at and fix the print_backwards function

37
00:01:44,320 --> 00:01:47,990
so that it correctly handles the case when the calling code

38
00:01:47,990 --> 00:01:51,190
also specifies the end keyword argument.

39
00:01:51,190 --> 00:01:52,790
And just as a tip here, you may want to remove

40
00:01:52,790 --> 00:01:55,760
the file=backwards from the call to print_backwards,

41
00:01:55,760 --> 00:01:57,760
just so you don't have to keep opening the file

42
00:01:57,760 --> 00:01:58,870
to check the results.

43
00:01:58,870 --> 00:02:01,290
Instead, getting the output on the screen.

44
00:02:01,290 --> 00:02:03,320
So that's your challenge, see how you go with it.

45
00:02:03,320 --> 00:02:07,530
Pause the video and I'll see you when you get back.

46
00:02:07,530 --> 00:02:08,970
Okay, so how did you get on?

47
00:02:08,970 --> 00:02:10,970
Hopefully you managed to solve that.

48
00:02:10,970 --> 00:02:13,620
And as I mentioned, there's actually two solutions to this,

49
00:02:13,620 --> 00:02:17,780
or two ways to deal with it, and I'm gonna show you both.

50
00:02:17,780 --> 00:02:20,710
Both of the solutions rely on making sure

51
00:02:20,710 --> 00:02:24,830
that we don't parse the end key word argument in kwargs.

52
00:02:24,830 --> 00:02:27,650
One approach is to actually include end

53
00:02:27,650 --> 00:02:29,390
in our parameter list.

54
00:02:29,390 --> 00:02:32,470
If we come up here and after the *args

55
00:02:32,470 --> 00:02:34,490
and before the **kwargs,

56
00:02:34,490 --> 00:02:38,450
we can actually put end= and then our space

57
00:02:38,450 --> 00:02:41,990
in single quotes and then a comma and a space

58
00:02:41,990 --> 00:02:43,820
and then our **kwargs.

59
00:02:45,410 --> 00:02:49,650
Just like with *args, **kwargs will actually scoop up

60
00:02:49,650 --> 00:02:51,830
any remaining key word arguments

61
00:02:51,830 --> 00:02:55,200
that we don't specify in our function definition.

62
00:02:55,200 --> 00:02:57,060
So that's the reason that I'm putting the end=

63
00:02:57,060 --> 00:02:58,600
a bit prior to that.

64
00:02:58,600 --> 00:03:01,220
Now, there is a slight difference though.

65
00:03:01,220 --> 00:03:03,630
*args deals with positional arguments.

66
00:03:03,630 --> 00:03:04,710
If they don't have names,

67
00:03:04,710 --> 00:03:07,310
so they just appear in order in the function call.

68
00:03:07,310 --> 00:03:10,220
But **kwargs uses the parameter names,

69
00:03:10,220 --> 00:03:12,630
so it doesn't care what order we specify them in

70
00:03:12,630 --> 00:03:14,400
when calling the function.

71
00:03:14,400 --> 00:03:17,210
It just scoops up any that our function hasn't declared

72
00:03:17,210 --> 00:03:19,350
and puts them into kwargs.

73
00:03:19,350 --> 00:03:21,330
So before running the programme,

74
00:03:21,330 --> 00:03:23,960
I'm gonna remove the file=backwards argument

75
00:03:23,960 --> 00:03:27,380
just so we can see the results on screen.

76
00:03:30,150 --> 00:03:32,720
So we'll try running that now.

77
00:03:32,720 --> 00:03:34,790
Alright, so that's the first option.

78
00:03:34,790 --> 00:03:36,210
That works fine, as you can see there.

79
00:03:36,210 --> 00:03:38,070
There's now no errors showing.

80
00:03:38,070 --> 00:03:39,630
The other approach though is to remove any

81
00:03:39,630 --> 00:03:42,380
end key word argument from the kwargs dictionary

82
00:03:42,380 --> 00:03:45,380
before parsing it on to the built in print function.

83
00:03:45,380 --> 00:03:47,270
I'm gonna leave that first statement definition

84
00:03:47,270 --> 00:03:49,600
commented out for reference.

85
00:03:51,090 --> 00:03:54,260
So I'll take a copy of that, paste it.

86
00:03:55,450 --> 00:03:57,240
And I'll comment out the first one.

87
00:03:57,240 --> 00:03:58,740
So I'm gonna change this,

88
00:03:58,740 --> 00:04:00,850
and we're gonna come down here to,

89
00:04:00,850 --> 00:04:03,010
after we do the print of kwargs,

90
00:04:03,010 --> 00:04:06,510
we're going to do kwr, so kwargs, dot pop,

91
00:04:09,110 --> 00:04:13,270
single quotes, end, comma, space, none with a capital N.

92
00:04:15,220 --> 00:04:16,420
So kwargs is just a dictionary,

93
00:04:16,420 --> 00:04:18,180
so we're free to manipulate its contents

94
00:04:18,180 --> 00:04:20,350
just like any other dictionary.

95
00:04:20,350 --> 00:04:22,860
I've used the dictionary pop method here.

96
00:04:22,860 --> 00:04:26,830
Now, if we click on pop and have a look at that,

97
00:04:26,830 --> 00:04:28,270
you can see that it removes

98
00:04:28,270 --> 00:04:30,400
the specified key from the dictionary.

99
00:04:30,400 --> 00:04:33,570
And we're providing the default value of none

100
00:04:33,570 --> 00:04:36,960
to prevent an error if end isn't already in the dictionary.

101
00:04:36,960 --> 00:04:40,530
Now, we could use other methods for removing the key,

102
00:04:40,530 --> 00:04:41,970
such as the del command,

103
00:04:41,970 --> 00:04:44,740
but just remember that the end argument may not exist,

104
00:04:44,740 --> 00:04:47,400
so your code has to check it before removing it

105
00:04:47,400 --> 00:04:49,710
or catch the key error exception.

106
00:04:49,710 --> 00:04:53,250
Alright, so we'll go back and check this code.

107
00:04:53,250 --> 00:04:54,080
And run.

108
00:04:56,260 --> 00:05:00,100
We can see that it too is also working just fine.

109
00:05:00,100 --> 00:05:01,860
Alright, so if you use one of those approaches

110
00:05:01,860 --> 00:05:03,910
to solving the challenge, then well done.

111
00:05:03,910 --> 00:05:05,770
And if you found another approach that also works,

112
00:05:05,770 --> 00:05:06,730
please let us know.

113
00:05:06,730 --> 00:05:08,140
That'd be very interesting.

114
00:05:08,140 --> 00:05:10,250
Alright, so that challenge was intended to show you

115
00:05:10,250 --> 00:05:12,310
how you can manipulate kwargs,

116
00:05:12,310 --> 00:05:15,100
either by providing the key word in your own definition

117
00:05:15,100 --> 00:05:18,280
or explicitly removing it from the dictionary.

118
00:05:18,280 --> 00:05:21,030
Now, our print_backwards function at the moment

119
00:05:21,030 --> 00:05:24,630
doesn't really behave like the built in print function.

120
00:05:24,630 --> 00:05:26,460
We've got working code, but it doesn't behave

121
00:05:26,460 --> 00:05:29,400
in the way that the calling code would expect.

122
00:05:29,400 --> 00:05:31,350
So to see what I mean there,

123
00:05:31,350 --> 00:05:33,090
let's press another string immediately

124
00:05:33,090 --> 00:05:35,670
after the print_backwards call.

125
00:05:37,270 --> 00:05:40,440
We'll just do a print, another string.

126
00:05:41,460 --> 00:05:43,730
And if we actually run that,

127
00:05:43,730 --> 00:05:45,620
you can see we've got another string

128
00:05:45,620 --> 00:05:47,840
sort of showing on the same line,

129
00:05:47,840 --> 00:05:49,360
right at the end of our backward string

130
00:05:49,360 --> 00:05:51,190
instead of on a new line.

131
00:05:51,190 --> 00:05:55,150
We're effectively using the end argument as a separator.

132
00:05:55,150 --> 00:05:57,560
Because we're using multiple calls to the built in

133
00:05:57,560 --> 00:05:59,660
print function inside our loop,

134
00:05:59,660 --> 00:06:02,290
we have to use end to prevent each word

135
00:06:02,290 --> 00:06:04,030
appearing on a new line.

136
00:06:04,030 --> 00:06:06,040
Now if we specify sep when calling

137
00:06:06,040 --> 00:06:09,860
our print_backwards function, it actually won't do anything.

138
00:06:09,860 --> 00:06:12,140
So let's compare our code to the built in one,

139
00:06:12,140 --> 00:06:14,980
when a separator's used in the function call.

140
00:06:14,980 --> 00:06:17,420
I'm gonna add this code outside the file loop

141
00:06:17,420 --> 00:06:20,620
and print a blank line first to separate the output.

142
00:06:20,620 --> 00:06:24,350
So let's have a go at doing that down here.

143
00:06:24,350 --> 00:06:26,950
So we're gonna do print to move to a new line

144
00:06:26,950 --> 00:06:29,180
before we start and then print,

145
00:06:29,180 --> 00:06:30,620
and I might take a copy of this again,

146
00:06:30,620 --> 00:06:32,790
just to save us typing it.

147
00:06:35,410 --> 00:06:38,000
We're gonna leave the end in there like so,

148
00:06:38,000 --> 00:06:40,110
but then we're also gonna introduce the sep,

149
00:06:40,110 --> 00:06:43,270
so sep= and we're going to use a pipe character,

150
00:06:43,270 --> 00:06:46,220
and then we're going to come up here

151
00:06:46,220 --> 00:06:48,450
and do an equivalent thing,

152
00:06:48,450 --> 00:06:51,630
but I'm going to just call the print function.

153
00:06:51,630 --> 00:06:54,890
So print in this case, exactly the same as the call

154
00:06:54,890 --> 00:06:56,870
on the line below to print_backwards,

155
00:06:56,870 --> 00:07:00,870
but we're just calling the print function instead.

156
00:07:00,870 --> 00:07:03,710
And let's just try running it now.

157
00:07:04,730 --> 00:07:07,390
So you can see in the version, the print version,

158
00:07:07,390 --> 00:07:10,230
we've got the pipe character working quite nicely.

159
00:07:10,230 --> 00:07:12,830
It's between each word, but on our version,

160
00:07:12,830 --> 00:07:16,070
the version that's calling the print_backwards function,

161
00:07:16,070 --> 00:07:18,990
the separator, the pipe character, isn't working.

162
00:07:18,990 --> 00:07:21,550
We've still got the space only working.

163
00:07:21,550 --> 00:07:22,830
So the point I wanna make here is that

164
00:07:22,830 --> 00:07:25,820
kwargs is very useful, but you also have to be careful

165
00:07:25,820 --> 00:07:27,790
that you don't subvert the meaning of the arguments

166
00:07:27,790 --> 00:07:29,590
that you're parsing on.

167
00:07:29,590 --> 00:07:31,280
Now we could fix this by joining the reverse

168
00:07:31,280 --> 00:07:34,090
words up into a string and using a single call

169
00:07:34,090 --> 00:07:36,330
to built in print function.

170
00:07:36,330 --> 00:07:38,730
In fact, that's probably the way I'd recommend it.

171
00:07:38,730 --> 00:07:40,450
The code would end up being a lot cleaner

172
00:07:40,450 --> 00:07:43,360
and more obvious with what's going on.

173
00:07:43,360 --> 00:07:45,910
But this video is about using args and kwargs,

174
00:07:45,910 --> 00:07:49,430
so I'm going to manipulate the key word arguments instead.

175
00:07:49,430 --> 00:07:51,220
You may find yourself having to do this

176
00:07:51,220 --> 00:07:52,930
when subclassing another class,

177
00:07:52,930 --> 00:07:55,400
so it's also useful to see how to do it.

178
00:07:55,400 --> 00:07:57,890
But what we're going to do is use any sep argument

179
00:07:57,890 --> 00:08:01,540
that the calling code provides instead of hard coding end

180
00:08:01,540 --> 00:08:05,610
to be a space, which it's currently doing on line six.

181
00:08:05,610 --> 00:08:07,780
So you can see it's hard coding the end there.

182
00:08:07,780 --> 00:08:09,960
And once we've done that, we'll add a final print call

183
00:08:09,960 --> 00:08:12,500
using whatever end value has been provided.

184
00:08:12,500 --> 00:08:15,060
Now I'm gonna copy the print_backwards function

185
00:08:15,060 --> 00:08:17,450
and comment out the original so the code is still available

186
00:08:17,450 --> 00:08:20,340
in the resources section of this video.

187
00:08:20,340 --> 00:08:21,840
So let me do that.

188
00:08:23,810 --> 00:08:25,770
Copy that and then we'll comment that out

189
00:08:25,770 --> 00:08:28,520
and then come down here and we'll paste it in again

190
00:08:28,520 --> 00:08:31,750
and then we'll make the changes to that.

191
00:08:31,750 --> 00:08:34,750
And two lines there to keep the editor happy.

192
00:08:34,750 --> 00:08:36,190
Alright, so firstly, what we're going to do

193
00:08:36,190 --> 00:08:37,990
is delete these two lines here,

194
00:08:37,990 --> 00:08:40,929
the print kwargs and the kwargs.pop

195
00:08:40,929 --> 00:08:43,049
and then we wanna add something.

196
00:08:43,049 --> 00:08:47,250
After here, we're going to then add end_character

197
00:08:47,250 --> 00:08:51,000
is equal to kwargs.pop end, then the slash N,

198
00:08:57,390 --> 00:09:00,350
single quotes, and then set_character

199
00:09:00,350 --> 00:09:03,600
is equal to kwargs.pop, and that's sep,

200
00:09:07,450 --> 00:09:09,370
and then a space in single quotes.

201
00:09:09,370 --> 00:09:12,430
And then we're gonna come down to our call to print here.

202
00:09:12,430 --> 00:09:15,750
We're gonna change end= and single quote and space

203
00:09:15,750 --> 00:09:17,770
that was in there, we're gonna change

204
00:09:17,770 --> 00:09:20,270
that instead to sep_character.

205
00:09:22,610 --> 00:09:25,330
They're still needing **kwargs on the end of that,

206
00:09:25,330 --> 00:09:26,660
and then on the line after that

207
00:09:26,660 --> 00:09:28,280
we're going to do a print.

208
00:09:28,280 --> 00:09:29,800
That's going to be after the (mumbling) is finished,

209
00:09:29,800 --> 00:09:32,630
so print, end= end_character.

210
00:09:37,340 --> 00:09:40,260
Alright, so let's just try running that.

211
00:09:40,260 --> 00:09:43,700
You can see now it's actually looking a lot better.

212
00:09:43,700 --> 00:09:45,370
It's now behaving almost the same as

213
00:09:45,370 --> 00:09:47,220
the built in print function.

214
00:09:47,220 --> 00:09:50,140
And it also works if we suppress the end of line character.

215
00:09:50,140 --> 00:09:53,390
Let's have a go at doing that just to see if it works.

216
00:09:53,390 --> 00:09:56,370
So first thing we're going to do here in this first print

217
00:09:56,370 --> 00:09:59,270
is we're going to change the end=

218
00:09:59,270 --> 00:10:01,300
and we're gonna remove the new line

219
00:10:01,300 --> 00:10:03,860
so it's just two single quotes.

220
00:10:03,860 --> 00:10:07,800
And we're also gonna do that on the regular or the,

221
00:10:07,800 --> 00:10:09,280
call to our method as well,

222
00:10:09,280 --> 00:10:12,130
so both the print and the call to our method.

223
00:10:12,130 --> 00:10:13,480
And actually what we'll do is we'll also change

224
00:10:13,480 --> 00:10:17,650
the separator this time to a /n for new line, **/n

225
00:10:20,460 --> 00:10:22,960
and we'll do the same for the function call

226
00:10:22,960 --> 00:10:27,130
to print_backwards, so separator= /n **/n.

227
00:10:28,700 --> 00:10:30,590
And then just to make it clear,

228
00:10:30,590 --> 00:10:34,760
we'll add a bit of text to separate it, so print= times 10.

229
00:10:38,500 --> 00:10:40,500
Right, so we're on that.

230
00:10:45,500 --> 00:10:47,910
So now the first word of our backward string starts

231
00:10:47,910 --> 00:10:51,060
on the same line as the last word on the print function.

232
00:10:51,060 --> 00:10:54,530
I've got the equals printout showing as the,

233
00:10:54,530 --> 00:10:57,720
well, after rather, the last backwards word.

234
00:10:57,720 --> 00:10:59,870
Looking at that, this is the code here

235
00:10:59,870 --> 00:11:01,430
that I'm talking about or the output.

236
00:11:01,430 --> 00:11:02,970
So the first word of our backwards string

237
00:11:02,970 --> 00:11:04,970
starts on the same line as the last word

238
00:11:04,970 --> 00:11:06,150
from the print function.

239
00:11:06,150 --> 00:11:07,820
That's the point really there.

240
00:11:07,820 --> 00:11:09,400
But then the line of equals

241
00:11:09,400 --> 00:11:11,720
does appear after our last backwards word,

242
00:11:11,720 --> 00:11:14,610
so print is correctly honouring the end of line characters

243
00:11:14,610 --> 00:11:17,840
but our print character, our print_backwards function

244
00:11:17,840 --> 00:11:19,550
doesn't cope when the end of line

245
00:11:19,550 --> 00:11:21,830
is suppressed, in this case.

246
00:11:21,830 --> 00:11:23,700
So as you can see, if you change the meaning

247
00:11:23,700 --> 00:11:27,010
of a key word argument, things can get a bit tricky.

248
00:11:27,010 --> 00:11:28,920
Now I probably wouldn't code this function

249
00:11:28,920 --> 00:11:30,650
using repeated calls to print.

250
00:11:30,650 --> 00:11:33,070
It's easier to join the backwards words

251
00:11:33,070 --> 00:11:35,590
using the join method that we've used before,

252
00:11:35,590 --> 00:11:38,650
then just call print once to print out the final string.

253
00:11:38,650 --> 00:11:40,890
But I can't leave you with an always working function

254
00:11:40,890 --> 00:11:43,380
so I'm gonna fix this one before ending the video.

255
00:11:43,380 --> 00:11:44,750
What we need to do though is handle

256
00:11:44,750 --> 00:11:47,640
the last word that we print a bit differently,

257
00:11:47,640 --> 00:11:49,490
so in fact it goes to a new line.

258
00:11:49,490 --> 00:11:51,060
The last word should be printed using

259
00:11:51,060 --> 00:11:54,500
the specified end argument rather than a separator.

260
00:11:54,500 --> 00:11:57,410
Now that's actually easier to do in Python.

261
00:11:57,410 --> 00:12:00,380
We just alter the range so that it reads the word backwards

262
00:12:00,380 --> 00:12:02,640
but stops before it prints the last one,

263
00:12:02,640 --> 00:12:04,330
which in this case is the first one

264
00:12:04,330 --> 00:12:05,950
because we're going backwards.

265
00:12:05,950 --> 00:12:08,890
Then we print the first word in args separately

266
00:12:08,890 --> 00:12:12,810
using the end value that the calling code specified.

267
00:12:12,810 --> 00:12:15,030
So let's have a look and see how to do that.

268
00:12:15,030 --> 00:12:17,350
Back to our definition here.

269
00:12:17,350 --> 00:12:18,970
The first that we've gotta do is change this

270
00:12:18,970 --> 00:12:20,430
full word in args.

271
00:12:20,430 --> 00:12:22,790
At the moment we've got ::-1.

272
00:12:22,790 --> 00:12:24,680
What we're gonna do, as I mentioned there,

273
00:12:24,680 --> 00:12:26,880
about changing the range, so we're gonna change

274
00:12:26,880 --> 00:12:28,710
that to, I'd be :0:-1.

275
00:12:35,580 --> 00:12:39,750
And then we're going to leave this coding as is on line 13.

276
00:12:40,680 --> 00:12:43,320
And then we're gonna come down here to the end here,

277
00:12:43,320 --> 00:12:45,440
print, and we're going to actually change that,

278
00:12:45,440 --> 00:12:47,510
so that we're gonna do a print.

279
00:12:47,510 --> 00:12:49,330
We'll actually comment that out, (mumbling)

280
00:12:49,330 --> 00:12:52,220
what it was before we're making this change.

281
00:12:52,220 --> 00:12:55,140
We're gonna do a print, args, zero,

282
00:12:57,660 --> 00:13:01,760
in square brackets, then another left square bracket, ::-1

283
00:13:01,760 --> 00:13:05,610
and then right square bracket, comma, end=

284
00:13:05,610 --> 00:13:08,690
and it's end_character then **kwargs.

285
00:13:11,390 --> 00:13:12,230
What we're doing here is effectively, we're uh,

286
00:13:12,230 --> 00:13:15,310
then print the first word separately.

287
00:13:20,630 --> 00:13:24,800
And by the way, as a result of that, that means...

288
00:13:29,640 --> 00:13:31,640
I'm gonna take a bit of a comment here,

289
00:13:31,640 --> 00:13:32,660
make it clear what we're doing,

290
00:13:32,660 --> 00:13:35,990
and here what we were doing on course was change the range.

291
00:13:35,990 --> 00:13:38,280
So those comments are meant to be read altogether.

292
00:13:38,280 --> 00:13:39,180
So I've changed the range,

293
00:13:39,180 --> 00:13:40,670
and print the first word separately

294
00:13:40,670 --> 00:13:42,300
which means we don't need this line,

295
00:13:42,300 --> 00:13:44,520
so that's why I've commented that out.

296
00:13:44,520 --> 00:13:46,360
Alright, so let's try running that.

297
00:13:46,360 --> 00:13:49,120
And the other thing I should have done previously

298
00:13:49,120 --> 00:13:53,200
was also remove this so I'm going to do that now.

299
00:13:54,480 --> 00:13:56,230
We'll run this again.

300
00:13:57,150 --> 00:13:58,760
That's actually what I was trying to do,

301
00:13:58,760 --> 00:14:01,600
because obviously we changed our functionality

302
00:14:01,600 --> 00:14:04,140
so we weren't using end anymore and I've removed that now

303
00:14:04,140 --> 00:14:07,230
and this is what was actually meant to happen.

304
00:14:07,230 --> 00:14:10,350
You can now see the equals sign is peering on the same line

305
00:14:10,350 --> 00:14:13,820
as our hollow going backwards (mumbling).

306
00:14:13,820 --> 00:14:17,640
So I used zero as the stock value in the code on line 12,

307
00:14:17,640 --> 00:14:18,750
as you can see here.

308
00:14:18,750 --> 00:14:21,140
Remember that the slides will extend up to

309
00:14:21,140 --> 00:14:23,000
but not including the stock value

310
00:14:23,000 --> 00:14:25,800
so therefore the first word isn't included.

311
00:14:25,800 --> 00:14:27,940
I'm gonna leave you to rerun the previous tests

312
00:14:27,940 --> 00:14:29,830
to confirm that our function now behaves

313
00:14:29,830 --> 00:14:31,950
the same as the built in print

314
00:14:31,950 --> 00:14:34,950
with different values for end and sep.

315
00:14:34,950 --> 00:14:37,180
So we can create new functions that are called

316
00:14:37,180 --> 00:14:39,950
in exactly the same way as an existing function

317
00:14:39,950 --> 00:14:43,010
but then change the way that the original function works.

318
00:14:43,010 --> 00:14:45,080
And we do this by retrieving the arguments

319
00:14:45,080 --> 00:14:49,080
using args, or using *args, I should say, and **kwargs

320
00:14:49,080 --> 00:14:52,640
to provide a version of the function of different behaviour.

321
00:14:52,640 --> 00:14:56,160
We can do the same thing with classes to create a subclass

322
00:14:56,160 --> 00:14:59,150
that adds additional functionality to the bass class

323
00:14:59,150 --> 00:15:02,500
and we'll be doing that actually in the next video.

324
00:15:02,500 --> 00:15:03,960
I used the print function here

325
00:15:03,960 --> 00:15:05,720
because it's a visual function,

326
00:15:05,720 --> 00:15:07,620
so it's easier to see what's going on.

327
00:15:07,620 --> 00:15:09,920
The aim was to show you how to use and manipulate

328
00:15:09,920 --> 00:15:12,490
the kwargs dictionary and how it's important

329
00:15:12,490 --> 00:15:16,320
to retain the expected behaviour of the original function.

330
00:15:16,320 --> 00:15:17,980
Now I've said a few times that if the aim

331
00:15:17,980 --> 00:15:20,550
was to create a version of print that prints backwards,

332
00:15:20,550 --> 00:15:22,220
then I wouldn't be doing it this way.

333
00:15:22,220 --> 00:15:24,150
I'm gonna finish the video by showing you

334
00:15:24,150 --> 00:15:27,150
the code I would write if the aim had been

335
00:15:27,150 --> 00:15:28,640
just to print backwards.

336
00:15:28,640 --> 00:15:30,090
Let me go ahead now and add that

337
00:15:30,090 --> 00:15:33,170
just to show you how that would work.

338
00:15:34,900 --> 00:15:39,070
And it would be def backwards_print *args comma and **kwargs

339
00:15:44,000 --> 00:15:46,920
sep_character equals kwargs.pop sep

340
00:15:53,750 --> 00:15:55,750
and then the space in single quotes,

341
00:15:55,750 --> 00:15:59,420
then we'll do print, sep_character.join word

342
00:16:06,970 --> 00:16:10,720
and in square brackets, ::-1 for word in args

343
00:16:13,990 --> 00:16:16,830
and then left square bracket, ::-1

344
00:16:16,830 --> 00:16:20,750
then a right square bracket, a right parenthesis,

345
00:16:20,750 --> 00:16:24,910
comma, **kwargs and then a final right parenthesis.

346
00:16:27,910 --> 00:16:29,880
Now backwards_print, as you can see here,

347
00:16:29,880 --> 00:16:32,750
joins the reversed words using the join method.

348
00:16:32,750 --> 00:16:34,720
So this would be the way that I would normally do it

349
00:16:34,720 --> 00:16:37,220
and it actually uses something called a list comprehension

350
00:16:37,220 --> 00:16:39,120
which we haven't looked at yet

351
00:16:39,120 --> 00:16:41,240
but we'll be looking at those later in the course.

352
00:16:41,240 --> 00:16:43,790
So again, if the aim was to create a version of print

353
00:16:43,790 --> 00:16:45,500
that prints backwards, I wouldn't have done it

354
00:16:45,500 --> 00:16:47,440
the way that I've shown you earlier in the video.

355
00:16:47,440 --> 00:16:49,400
We've done it that way just to really explore

356
00:16:49,400 --> 00:16:52,390
what kwargs is and args and how to use them.

357
00:16:52,390 --> 00:16:54,240
Anyway, I'll leave it there

358
00:16:54,240 --> 00:16:57,240
and we'll see you in the next video.

