WEBVTT 1 00:00:02.013 --> 00:00:03.307 All right, so as I mentioned 2 00:00:03.307 --> 00:00:05.093 towards the end of the last video, 3 00:00:05.093 --> 00:00:07.221 there's a slight problem with this code 4 00:00:07.221 --> 00:00:09.877 and I'm gonna get you to look at a challenge 5 00:00:09.877 --> 00:00:11.709 of fixing that shortly, 6 00:00:11.709 --> 00:00:15.369 but before we do that, let's see what the actual problem is. 7 00:00:15.369 --> 00:00:17.709 So what happens if the calling code specifies 8 00:00:17.709 --> 00:00:21.665 a key word argument that our function is already using? 9 00:00:21.665 --> 00:00:24.742 So in our code, we need to suppress the end of line 10 00:00:24.742 --> 00:00:28.202 character when we call the built in print function. 11 00:00:28.202 --> 00:00:29.822 So we're parsing, as you can see here, 12 00:00:29.822 --> 00:00:33.152 the end equals on line four with the little space there 13 00:00:33.152 --> 00:00:35.416 and I'm going to actually show you, 14 00:00:35.416 --> 00:00:37.920 I'm gonna change the calling code on line seven 15 00:00:37.920 --> 00:00:40.832 to put to also specify the end argument, 16 00:00:40.832 --> 00:00:42.972 which would be something that ordinarily 17 00:00:42.972 --> 00:00:45.148 would be a perfectly valid thing to do. 18 00:00:45.148 --> 00:00:48.197 So if I change that and put end= 19 00:00:48.197 --> 00:00:51.104 single quotes, a new line character. 20 00:00:51.104 --> 00:00:53.687 So if we actually run this now. 21 00:00:56.271 --> 00:00:57.799 So what's happening here is you can see 22 00:00:57.799 --> 00:01:01.105 there's an error is that kwargs contains the end argument. 23 00:01:01.105 --> 00:01:04.165 You can see that I'm calling that there. 24 00:01:04.165 --> 00:01:07.389 You can see that's showing in the printout of kwargs 25 00:01:07.389 --> 00:01:10.465 that we're doing on line two in the console output. 26 00:01:10.465 --> 00:01:12.609 But then we're also providing a value for it 27 00:01:12.609 --> 00:01:14.197 here on line four. 28 00:01:14.197 --> 00:01:17.109 You can't actually specify the same key word twice, 29 00:01:17.109 --> 00:01:18.804 and that's the problem. 30 00:01:18.804 --> 00:01:21.609 The specific error, got multiple valleys 31 00:01:21.609 --> 00:01:23.658 for key word argument end. 32 00:01:23.658 --> 00:01:25.937 And consequently we're getting a crash for that. 33 00:01:25.937 --> 00:01:27.890 Alright, so there's two ways to deal with this 34 00:01:27.890 --> 00:01:32.079 and I'm gonna show both in your solution to the challenge. 35 00:01:32.079 --> 00:01:34.579 Okay, but onto your challenge. 36 00:01:37.174 --> 00:01:41.074 You need to look at and fix the print_backwards function 37 00:01:41.074 --> 00:01:44.746 so that it correctly handles the case when the calling code 38 00:01:44.746 --> 00:01:47.944 also specifies the end keyword argument. 39 00:01:47.944 --> 00:01:49.541 And just as a tip here, you may want to remove 40 00:01:49.541 --> 00:01:52.510 the file=backwards from the call to print_backwards, 41 00:01:52.510 --> 00:01:54.517 just so you don't have to keep opening the file 42 00:01:54.517 --> 00:01:55.629 to check the results. 43 00:01:55.629 --> 00:01:58.042 Instead, getting the output on the screen. 44 00:01:58.042 --> 00:02:00.070 So that's your challenge, see how you go with it. 45 00:02:00.070 --> 00:02:04.286 Pause the video and I'll see you when you get back. 46 00:02:04.286 --> 00:02:05.726 Okay, so how did you get on? 47 00:02:05.726 --> 00:02:07.720 Hopefully you managed to solve that. 48 00:02:07.720 --> 00:02:10.376 And as I mentioned, there's actually two solutions to this, 49 00:02:10.376 --> 00:02:14.534 or two ways to deal with it, and I'm gonna show you both. 50 00:02:14.534 --> 00:02:17.461 Both of the solutions rely on making sure 51 00:02:17.461 --> 00:02:21.586 that we don't parse the end key word argument in kwargs. 52 00:02:21.586 --> 00:02:24.406 One approach is to actually include end 53 00:02:24.406 --> 00:02:26.146 in our parameter list. 54 00:02:26.146 --> 00:02:29.222 If we come up here and after the *args 55 00:02:29.222 --> 00:02:31.243 and before the **kwargs, 56 00:02:31.243 --> 00:02:35.205 we can actually put end= and then our space 57 00:02:35.205 --> 00:02:38.744 in single quotes and then a comma and a space 58 00:02:38.744 --> 00:02:40.577 and then our **kwargs. 59 00:02:42.160 --> 00:02:46.408 Just like with *args, **kwargs will actually scoop up 60 00:02:46.408 --> 00:02:48.580 any remaining key word arguments 61 00:02:48.580 --> 00:02:51.956 that we don't specify in our function definition. 62 00:02:51.956 --> 00:02:53.816 So that's the reason that I'm putting the end= 63 00:02:53.816 --> 00:02:55.353 a bit prior to that. 64 00:02:55.353 --> 00:02:57.976 Now, there is a slight difference though. 65 00:02:57.976 --> 00:03:00.388 *args deals with positional arguments. 66 00:03:00.388 --> 00:03:01.468 If they don't have names, 67 00:03:01.468 --> 00:03:04.064 so they just appear in order in the function call. 68 00:03:04.064 --> 00:03:06.972 But **kwargs uses the parameter names, 69 00:03:06.972 --> 00:03:09.386 so it doesn't care what order we specify them in 70 00:03:09.386 --> 00:03:11.159 when calling the function. 71 00:03:11.159 --> 00:03:13.964 It just scoops up any that our function hasn't declared 72 00:03:13.964 --> 00:03:16.108 and puts them into kwargs. 73 00:03:16.108 --> 00:03:18.088 So before running the programme, 74 00:03:18.088 --> 00:03:20.716 I'm gonna remove the file=backwards argument 75 00:03:20.716 --> 00:03:24.133 just so we can see the results on screen. 76 00:03:26.905 --> 00:03:29.472 So we'll try running that now. 77 00:03:29.472 --> 00:03:31.544 Alright, so that's the first option. 78 00:03:31.544 --> 00:03:32.968 That works fine, as you can see there. 79 00:03:32.968 --> 00:03:34.826 There's now no errors showing. 80 00:03:34.826 --> 00:03:36.388 The other approach though is to remove any 81 00:03:36.388 --> 00:03:39.132 end key word argument from the kwargs dictionary 82 00:03:39.132 --> 00:03:42.130 before parsing it on to the built in print function. 83 00:03:42.130 --> 00:03:44.024 I'm gonna leave that first statement definition 84 00:03:44.024 --> 00:03:46.357 commented out for reference. 85 00:03:47.848 --> 00:03:51.015 So I'll take a copy of that, paste it. 86 00:03:52.200 --> 00:03:53.996 And I'll comment out the first one. 87 00:03:53.996 --> 00:03:55.496 So I'm gonna change this, 88 00:03:55.496 --> 00:03:57.608 and we're gonna come down here to, 89 00:03:57.608 --> 00:03:59.768 after we do the print of kwargs, 90 00:03:59.768 --> 00:04:03.268 we're going to do kwr, so kwargs, dot pop, 91 00:04:05.860 --> 00:04:10.027 single quotes, end, comma, space, none with a capital N. 92 00:04:11.977 --> 00:04:13.177 So kwargs is just a dictionary, 93 00:04:13.177 --> 00:04:14.936 so we're free to manipulate its contents 94 00:04:14.936 --> 00:04:17.106 just like any other dictionary. 95 00:04:17.106 --> 00:04:19.616 I've used the dictionary pop method here. 96 00:04:19.616 --> 00:04:23.588 Now, if we click on pop and have a look at that, 97 00:04:23.588 --> 00:04:25.028 you can see that it removes 98 00:04:25.028 --> 00:04:27.159 the specified key from the dictionary. 99 00:04:27.159 --> 00:04:30.323 And we're providing the default value of none 100 00:04:30.323 --> 00:04:33.715 to prevent an error if end isn't already in the dictionary. 101 00:04:33.715 --> 00:04:37.283 Now, we could use other methods for removing the key, 102 00:04:37.283 --> 00:04:38.723 such as the del command, 103 00:04:38.723 --> 00:04:41.499 but just remember that the end argument may not exist, 104 00:04:41.499 --> 00:04:44.155 so your code has to check it before removing it 105 00:04:44.155 --> 00:04:46.461 or catch the key error exception. 106 00:04:46.461 --> 00:04:50.003 Alright, so we'll go back and check this code. 107 00:04:50.003 --> 00:04:50.836 And run. 108 00:04:53.019 --> 00:04:56.855 We can see that it too is also working just fine. 109 00:04:56.855 --> 00:04:58.611 Alright, so if you use one of those approaches 110 00:04:58.611 --> 00:05:00.667 to solving the challenge, then well done. 111 00:05:00.667 --> 00:05:02.527 And if you found another approach that also works, 112 00:05:02.527 --> 00:05:03.485 please let us know. 113 00:05:03.485 --> 00:05:04.895 That'd be very interesting. 114 00:05:04.895 --> 00:05:07.008 Alright, so that challenge was intended to show you 115 00:05:07.008 --> 00:05:09.067 how you can manipulate kwargs, 116 00:05:09.067 --> 00:05:11.859 either by providing the key word in your own definition 117 00:05:11.859 --> 00:05:15.039 or explicitly removing it from the dictionary. 118 00:05:15.039 --> 00:05:17.783 Now, our print_backwards function at the moment 119 00:05:17.783 --> 00:05:21.383 doesn't really behave like the built in print function. 120 00:05:21.383 --> 00:05:23.211 We've got working code, but it doesn't behave 121 00:05:23.211 --> 00:05:26.151 in the way that the calling code would expect. 122 00:05:26.151 --> 00:05:28.101 So to see what I mean there, 123 00:05:28.101 --> 00:05:29.843 let's press another string immediately 124 00:05:29.843 --> 00:05:32.426 after the print_backwards call. 125 00:05:34.027 --> 00:05:37.194 We'll just do a print, another string. 126 00:05:38.211 --> 00:05:40.489 And if we actually run that, 127 00:05:40.489 --> 00:05:42.377 you can see we've got another string 128 00:05:42.377 --> 00:05:44.597 sort of showing on the same line, 129 00:05:44.597 --> 00:05:46.113 right at the end of our backward string 130 00:05:46.113 --> 00:05:47.945 instead of on a new line. 131 00:05:47.945 --> 00:05:51.905 We're effectively using the end argument as a separator. 132 00:05:51.905 --> 00:05:54.317 Because we're using multiple calls to the built in 133 00:05:54.317 --> 00:05:56.417 print function inside our loop, 134 00:05:56.417 --> 00:05:59.045 we have to use end to prevent each word 135 00:05:59.045 --> 00:06:00.785 appearing on a new line. 136 00:06:00.785 --> 00:06:02.793 Now if we specify sep when calling 137 00:06:02.793 --> 00:06:06.617 our print_backwards function, it actually won't do anything. 138 00:06:06.617 --> 00:06:08.897 So let's compare our code to the built in one, 139 00:06:08.897 --> 00:06:11.730 when a separator's used in the function call. 140 00:06:11.730 --> 00:06:14.175 I'm gonna add this code outside the file loop 141 00:06:14.175 --> 00:06:17.373 and print a blank line first to separate the output. 142 00:06:17.373 --> 00:06:21.102 So let's have a go at doing that down here. 143 00:06:21.102 --> 00:06:23.701 So we're gonna do print to move to a new line 144 00:06:23.701 --> 00:06:25.937 before we start and then print, 145 00:06:25.937 --> 00:06:27.377 and I might take a copy of this again, 146 00:06:27.377 --> 00:06:29.544 just to save us typing it. 147 00:06:32.161 --> 00:06:34.757 We're gonna leave the end in there like so, 148 00:06:34.757 --> 00:06:36.865 but then we're also gonna introduce the sep, 149 00:06:36.865 --> 00:06:40.021 so sep= and we're going to use a pipe character, 150 00:06:40.021 --> 00:06:42.977 and then we're going to come up here 151 00:06:42.977 --> 00:06:45.205 and do an equivalent thing, 152 00:06:45.205 --> 00:06:48.387 but I'm going to just call the print function. 153 00:06:48.387 --> 00:06:51.645 So print in this case, exactly the same as the call 154 00:06:51.645 --> 00:06:53.621 on the line below to print_backwards, 155 00:06:53.621 --> 00:06:57.629 but we're just calling the print function instead. 156 00:06:57.629 --> 00:07:00.462 And let's just try running it now. 157 00:07:01.483 --> 00:07:04.141 So you can see in the version, the print version, 158 00:07:04.141 --> 00:07:06.989 we've got the pipe character working quite nicely. 159 00:07:06.989 --> 00:07:09.585 It's between each word, but on our version, 160 00:07:09.585 --> 00:07:12.825 the version that's calling the print_backwards function, 161 00:07:12.825 --> 00:07:15.749 the separator, the pipe character, isn't working. 162 00:07:15.749 --> 00:07:18.301 We've still got the space only working. 163 00:07:18.301 --> 00:07:19.585 So the point I wanna make here is that 164 00:07:19.585 --> 00:07:22.577 kwargs is very useful, but you also have to be careful 165 00:07:22.577 --> 00:07:24.541 that you don't subvert the meaning of the arguments 166 00:07:24.541 --> 00:07:26.341 that you're parsing on. 167 00:07:26.341 --> 00:07:28.037 Now we could fix this by joining the reverse 168 00:07:28.037 --> 00:07:30.841 words up into a string and using a single call 169 00:07:30.841 --> 00:07:33.085 to built in print function. 170 00:07:33.085 --> 00:07:35.485 In fact, that's probably the way I'd recommend it. 171 00:07:35.485 --> 00:07:37.201 The code would end up being a lot cleaner 172 00:07:37.201 --> 00:07:40.111 and more obvious with what's going on. 173 00:07:40.111 --> 00:07:42.663 But this video is about using args and kwargs, 174 00:07:42.663 --> 00:07:46.187 so I'm going to manipulate the key word arguments instead. 175 00:07:46.187 --> 00:07:47.970 You may find yourself having to do this 176 00:07:47.970 --> 00:07:49.688 when subclassing another class, 177 00:07:49.688 --> 00:07:52.158 so it's also useful to see how to do it. 178 00:07:52.158 --> 00:07:54.649 But what we're going to do is use any sep argument 179 00:07:54.649 --> 00:07:58.296 that the calling code provides instead of hard coding end 180 00:07:58.296 --> 00:08:02.361 to be a space, which it's currently doing on line six. 181 00:08:02.361 --> 00:08:04.537 So you can see it's hard coding the end there. 182 00:08:04.537 --> 00:08:06.710 And once we've done that, we'll add a final print call 183 00:08:06.710 --> 00:08:09.253 using whatever end value has been provided. 184 00:08:09.253 --> 00:08:11.817 Now I'm gonna copy the print_backwards function 185 00:08:11.817 --> 00:08:14.206 and comment out the original so the code is still available 186 00:08:14.206 --> 00:08:17.094 in the resources section of this video. 187 00:08:17.094 --> 00:08:18.594 So let me do that. 188 00:08:20.564 --> 00:08:22.524 Copy that and then we'll comment that out 189 00:08:22.524 --> 00:08:25.270 and then come down here and we'll paste it in again 190 00:08:25.270 --> 00:08:28.501 and then we'll make the changes to that. 191 00:08:28.501 --> 00:08:31.501 And two lines there to keep the editor happy. 192 00:08:31.501 --> 00:08:32.942 Alright, so firstly, what we're going to do 193 00:08:32.942 --> 00:08:34.749 is delete these two lines here, 194 00:08:34.749 --> 00:08:37.686 the print kwargs and the kwargs.pop 195 00:08:37.686 --> 00:08:39.804 and then we wanna add something. 196 00:08:39.804 --> 00:08:44.004 After here, we're going to then add end_character 197 00:08:44.004 --> 00:08:47.754 is equal to kwargs.pop end, then the slash N, 198 00:08:54.148 --> 00:08:57.107 single quotes, and then set_character 199 00:08:57.107 --> 00:09:00.357 is equal to kwargs.pop, and that's sep, 200 00:09:04.205 --> 00:09:06.124 and then a space in single quotes. 201 00:09:06.124 --> 00:09:09.187 And then we're gonna come down to our call to print here. 202 00:09:09.187 --> 00:09:12.508 We're gonna change end= and single quote and space 203 00:09:12.508 --> 00:09:14.521 that was in there, we're gonna change 204 00:09:14.521 --> 00:09:17.021 that instead to sep_character. 205 00:09:19.366 --> 00:09:22.084 They're still needing **kwargs on the end of that, 206 00:09:22.084 --> 00:09:23.410 and then on the line after that 207 00:09:23.410 --> 00:09:25.037 we're going to do a print. 208 00:09:25.037 --> 00:09:26.554 That's going to be after the (mumbling) is finished, 209 00:09:26.554 --> 00:09:29.387 so print, end= end_character. 210 00:09:34.099 --> 00:09:37.010 Alright, so let's just try running that. 211 00:09:37.010 --> 00:09:40.456 You can see now it's actually looking a lot better. 212 00:09:40.456 --> 00:09:42.122 It's now behaving almost the same as 213 00:09:42.122 --> 00:09:43.970 the built in print function. 214 00:09:43.970 --> 00:09:46.897 And it also works if we suppress the end of line character. 215 00:09:46.897 --> 00:09:50.145 Let's have a go at doing that just to see if it works. 216 00:09:50.145 --> 00:09:53.124 So first thing we're going to do here in this first print 217 00:09:53.124 --> 00:09:56.025 is we're going to change the end= 218 00:09:56.025 --> 00:09:58.057 and we're gonna remove the new line 219 00:09:58.057 --> 00:10:00.615 so it's just two single quotes. 220 00:10:00.615 --> 00:10:04.552 And we're also gonna do that on the regular or the, 221 00:10:04.552 --> 00:10:06.036 call to our method as well, 222 00:10:06.036 --> 00:10:08.882 so both the print and the call to our method. 223 00:10:08.882 --> 00:10:10.234 And actually what we'll do is we'll also change 224 00:10:10.234 --> 00:10:14.401 the separator this time to a /n for new line, **/n 225 00:10:17.211 --> 00:10:19.717 and we'll do the same for the function call 226 00:10:19.717 --> 00:10:23.884 to print_backwards, so separator= /n **/n. 227 00:10:25.458 --> 00:10:27.347 And then just to make it clear, 228 00:10:27.347 --> 00:10:31.514 we'll add a bit of text to separate it, so print= times 10. 229 00:10:35.258 --> 00:10:37.258 Right, so we're on that. 230 00:10:42.259 --> 00:10:44.666 So now the first word of our backward string starts 231 00:10:44.666 --> 00:10:47.812 on the same line as the last word on the print function. 232 00:10:47.812 --> 00:10:51.284 I've got the equals printout showing as the, 233 00:10:51.284 --> 00:10:54.476 well, after rather, the last backwards word. 234 00:10:54.476 --> 00:10:56.621 Looking at that, this is the code here 235 00:10:56.621 --> 00:10:58.188 that I'm talking about or the output. 236 00:10:58.188 --> 00:10:59.727 So the first word of our backwards string 237 00:10:59.727 --> 00:11:01.728 starts on the same line as the last word 238 00:11:01.728 --> 00:11:02.903 from the print function. 239 00:11:02.903 --> 00:11:04.573 That's the point really there. 240 00:11:04.573 --> 00:11:06.151 But then the line of equals 241 00:11:06.151 --> 00:11:08.478 does appear after our last backwards word, 242 00:11:08.478 --> 00:11:11.364 so print is correctly honouring the end of line characters 243 00:11:11.364 --> 00:11:14.595 but our print character, our print_backwards function 244 00:11:14.595 --> 00:11:16.307 doesn't cope when the end of line 245 00:11:16.307 --> 00:11:18.588 is suppressed, in this case. 246 00:11:18.588 --> 00:11:20.451 So as you can see, if you change the meaning 247 00:11:20.451 --> 00:11:23.766 of a key word argument, things can get a bit tricky. 248 00:11:23.766 --> 00:11:25.670 Now I probably wouldn't code this function 249 00:11:25.670 --> 00:11:27.406 using repeated calls to print. 250 00:11:27.406 --> 00:11:29.829 It's easier to join the backwards words 251 00:11:29.829 --> 00:11:32.344 using the join method that we've used before, 252 00:11:32.344 --> 00:11:35.403 then just call print once to print out the final string. 253 00:11:35.403 --> 00:11:37.649 But I can't leave you with an always working function 254 00:11:37.649 --> 00:11:40.132 so I'm gonna fix this one before ending the video. 255 00:11:40.132 --> 00:11:41.508 What we need to do though is handle 256 00:11:41.508 --> 00:11:44.393 the last word that we print a bit differently, 257 00:11:44.393 --> 00:11:46.247 so in fact it goes to a new line. 258 00:11:46.247 --> 00:11:47.815 The last word should be printed using 259 00:11:47.815 --> 00:11:51.252 the specified end argument rather than a separator. 260 00:11:51.252 --> 00:11:54.168 Now that's actually easier to do in Python. 261 00:11:54.168 --> 00:11:57.137 We just alter the range so that it reads the word backwards 262 00:11:57.137 --> 00:11:59.399 but stops before it prints the last one, 263 00:11:59.399 --> 00:12:01.081 which in this case is the first one 264 00:12:01.081 --> 00:12:02.705 because we're going backwards. 265 00:12:02.705 --> 00:12:05.643 Then we print the first word in args separately 266 00:12:05.643 --> 00:12:09.566 using the end value that the calling code specified. 267 00:12:09.566 --> 00:12:11.784 So let's have a look and see how to do that. 268 00:12:11.784 --> 00:12:14.107 Back to our definition here. 269 00:12:14.107 --> 00:12:15.729 The first that we've gotta do is change this 270 00:12:15.729 --> 00:12:17.183 full word in args. 271 00:12:17.183 --> 00:12:19.545 At the moment we've got ::-1. 272 00:12:19.545 --> 00:12:21.432 What we're gonna do, as I mentioned there, 273 00:12:21.432 --> 00:12:23.635 about changing the range, so we're gonna change 274 00:12:23.635 --> 00:12:25.468 that to, I'd be :0:-1. 275 00:12:32.335 --> 00:12:36.502 And then we're going to leave this coding as is on line 13. 276 00:12:37.439 --> 00:12:40.076 And then we're gonna come down here to the end here, 277 00:12:40.076 --> 00:12:42.193 print, and we're going to actually change that, 278 00:12:42.193 --> 00:12:44.267 so that we're gonna do a print. 279 00:12:44.267 --> 00:12:46.081 We'll actually comment that out, (mumbling) 280 00:12:46.081 --> 00:12:48.978 what it was before we're making this change. 281 00:12:48.978 --> 00:12:51.895 We're gonna do a print, args, zero, 282 00:12:54.417 --> 00:12:58.519 in square brackets, then another left square bracket, ::-1 283 00:12:58.519 --> 00:13:02.363 and then right square bracket, comma, end= 284 00:13:02.363 --> 00:13:05.446 and it's end_character then **kwargs. 285 00:13:08.148 --> 00:13:08.981 What we're doing here is effectively, we're uh, 286 00:13:08.981 --> 00:13:12.064 then print the first word separately. 287 00:13:17.389 --> 00:13:21.556 And by the way, as a result of that, that means... 288 00:13:26.399 --> 00:13:28.399 I'm gonna take a bit of a comment here, 289 00:13:28.399 --> 00:13:29.416 make it clear what we're doing, 290 00:13:29.416 --> 00:13:32.749 and here what we were doing on course was change the range. 291 00:13:32.749 --> 00:13:35.031 So those comments are meant to be read altogether. 292 00:13:35.031 --> 00:13:35.939 So I've changed the range, 293 00:13:35.939 --> 00:13:37.420 and print the first word separately 294 00:13:37.420 --> 00:13:39.056 which means we don't need this line, 295 00:13:39.056 --> 00:13:41.275 so that's why I've commented that out. 296 00:13:41.275 --> 00:13:43.116 Alright, so let's try running that. 297 00:13:43.116 --> 00:13:45.873 And the other thing I should have done previously 298 00:13:45.873 --> 00:13:49.956 was also remove this so I'm going to do that now. 299 00:13:51.236 --> 00:13:52.986 We'll run this again. 300 00:13:53.902 --> 00:13:55.512 That's actually what I was trying to do, 301 00:13:55.512 --> 00:13:58.354 because obviously we changed our functionality 302 00:13:58.354 --> 00:14:00.891 so we weren't using end anymore and I've removed that now 303 00:14:00.891 --> 00:14:03.980 and this is what was actually meant to happen. 304 00:14:03.980 --> 00:14:07.108 You can now see the equals sign is peering on the same line 305 00:14:07.108 --> 00:14:10.570 as our hollow going backwards (mumbling). 306 00:14:10.570 --> 00:14:14.398 So I used zero as the stock value in the code on line 12, 307 00:14:14.398 --> 00:14:15.503 as you can see here. 308 00:14:15.503 --> 00:14:17.894 Remember that the slides will extend up to 309 00:14:17.894 --> 00:14:19.752 but not including the stock value 310 00:14:19.752 --> 00:14:22.552 so therefore the first word isn't included. 311 00:14:22.552 --> 00:14:24.698 I'm gonna leave you to rerun the previous tests 312 00:14:24.698 --> 00:14:26.581 to confirm that our function now behaves 313 00:14:26.581 --> 00:14:28.700 the same as the built in print 314 00:14:28.700 --> 00:14:31.700 with different values for end and sep. 315 00:14:31.700 --> 00:14:33.932 So we can create new functions that are called 316 00:14:33.932 --> 00:14:36.700 in exactly the same way as an existing function 317 00:14:36.700 --> 00:14:39.762 but then change the way that the original function works. 318 00:14:39.762 --> 00:14:41.835 And we do this by retrieving the arguments 319 00:14:41.835 --> 00:14:45.837 using args, or using *args, I should say, and **kwargs 320 00:14:45.837 --> 00:14:49.398 to provide a version of the function of different behaviour. 321 00:14:49.398 --> 00:14:52.913 We can do the same thing with classes to create a subclass 322 00:14:52.913 --> 00:14:55.902 that adds additional functionality to the bass class 323 00:14:55.902 --> 00:14:59.250 and we'll be doing that actually in the next video. 324 00:14:59.250 --> 00:15:00.716 I used the print function here 325 00:15:00.716 --> 00:15:02.470 because it's a visual function, 326 00:15:02.470 --> 00:15:04.370 so it's easier to see what's going on. 327 00:15:04.370 --> 00:15:06.672 The aim was to show you how to use and manipulate 328 00:15:06.672 --> 00:15:09.242 the kwargs dictionary and how it's important 329 00:15:09.242 --> 00:15:13.070 to retain the expected behaviour of the original function. 330 00:15:13.070 --> 00:15:14.736 Now I've said a few times that if the aim 331 00:15:14.736 --> 00:15:17.304 was to create a version of print that prints backwards, 332 00:15:17.304 --> 00:15:18.970 then I wouldn't be doing it this way. 333 00:15:18.970 --> 00:15:20.900 I'm gonna finish the video by showing you 334 00:15:20.900 --> 00:15:23.900 the code I would write if the aim had been 335 00:15:23.900 --> 00:15:25.393 just to print backwards. 336 00:15:25.393 --> 00:15:26.843 Let me go ahead now and add that 337 00:15:26.843 --> 00:15:29.926 just to show you how that would work. 338 00:15:31.657 --> 00:15:35.824 And it would be def backwards_print *args comma and **kwargs 339 00:15:40.757 --> 00:15:43.674 sep_character equals kwargs.pop sep 340 00:15:50.502 --> 00:15:52.503 and then the space in single quotes, 341 00:15:52.503 --> 00:15:56.170 then we'll do print, sep_character.join word 342 00:16:03.728 --> 00:16:07.478 and in square brackets, ::-1 for word in args 343 00:16:10.746 --> 00:16:13.588 and then left square bracket, ::-1 344 00:16:13.588 --> 00:16:17.501 then a right square bracket, a right parenthesis, 345 00:16:17.501 --> 00:16:21.668 comma, **kwargs and then a final right parenthesis. 346 00:16:24.666 --> 00:16:26.638 Now backwards_print, as you can see here, 347 00:16:26.638 --> 00:16:29.507 joins the reversed words using the join method. 348 00:16:29.507 --> 00:16:31.479 So this would be the way that I would normally do it 349 00:16:31.479 --> 00:16:33.973 and it actually uses something called a list comprehension 350 00:16:33.973 --> 00:16:35.871 which we haven't looked at yet 351 00:16:35.871 --> 00:16:37.990 but we'll be looking at those later in the course. 352 00:16:37.990 --> 00:16:40.541 So again, if the aim was to create a version of print 353 00:16:40.541 --> 00:16:42.251 that prints backwards, I wouldn't have done it 354 00:16:42.251 --> 00:16:44.196 the way that I've shown you earlier in the video. 355 00:16:44.196 --> 00:16:46.153 We've done it that way just to really explore 356 00:16:46.153 --> 00:16:49.142 what kwargs is and args and how to use them. 357 00:16:49.142 --> 00:16:50.998 Anyway, I'll leave it there 358 00:16:50.998 --> 00:16:53.998 and we'll see you in the next video.