WEBVTT 1 00:00:01.960 --> 00:00:05.310 so in the previous we started talking about functions and i ended it 2 00:00:05.310 --> 00:00:10.260 by saying that we could add the functionality to our center 3 00:00:10.260 --> 00:00:16.450 text function and to do the same as the example on line 22 with 4 00:00:16.450 --> 00:00:19.869 the 5 parameters that variable number of parameters that are pass currently to the 5 00:00:19.869 --> 00:00:24.810 print function what we can do the way we can do that is we can use the star 6 00:00:24.810 --> 00:00:27.649 talked about for arguments and then iterate through the argument list and 7 00:00:27.649 --> 00:00:31.570 concatenate all the arguments into one string before centering it so what we need to is 8 00:00:31.570 --> 00:00:34.660 make a change here we need to use the Asterix and if you remembered by looking at 9 00:00:34.660 --> 00:00:39.910 the built-ins doc string for print we knew that by adding an asterix that 10 00:00:39.910 --> 00:00:44.500 signifies Python their is as a variable number of parameters so then what you need to do is 11 00:00:44.500 --> 00:00:47.699 we can't just put string equals text because we can now have multiple parameters 12 00:00:47.699 --> 00:00:53.539 so what we need to do is start off by making an empty string and we need to 13 00:00:53.539 --> 00:01:00.940 create a for loop and actually add our parameters so put..... 14 00:01:00.940 --> 00:01:08.750 and args we are gonna change this up here to be the parameters so in this case the arguments and put..... 15 00:01:08.750 --> 00:01:17.200 ...and once we do that of course 16 00:01:17.200 --> 00:01:22.560 the indents back on line 12 to the code we're going to have a text variable 17 00:01:22.560 --> 00:01:26.189 which contains all the arguments and it should work quite nicely without any 18 00:01:26.189 --> 00:01:30.750 code modification in terms of calling the codes so if we run this we should find 19 00:01:30.750 --> 00:01:35.219 that it still works still works as it is with one parameter but if we then changed 20 00:01:35.219 --> 00:01:44.060 this....and we do something like... 21 00:01:44.060 --> 00:01:50.299 ...so we've got 5 paramteres and we've 22 00:01:50.299 --> 00:01:54.509 got a numbers and strings we can use their so if we run that you will find in the 23 00:01:54.509 --> 00:01:59.369 last line first second 3 4 spam so it quite happily accepted the arguments and centered them 24 00:01:59.369 --> 00:02:02.579 as you can see on the screen their now I have said previously that concatenate strings 25 00:02:02.579 --> 00:02:06.299 in a list is not very efficient and there is a more efficient way of doing 26 00:02:06.299 --> 00:02:09.840 this using list comprehensions but we're not really at that point at the 27 00:02:09.840 --> 00:02:13.090 moment we're going to cover those later in the course so we are not going to use one here 28 00:02:13.090 --> 00:02:16.819 just something to keep in mind that it isn't the most efficient way 29 00:02:16.819 --> 00:02:20.540 currently of concatenating strings and you might think ok maybe we can 30 00:02:20.540 --> 00:02:24.560 use the join here we can do something like.... 31 00:02:24.560 --> 00:02:37.549 so you might think we can go........but if you run that we get an error 32 00:02:37.549 --> 00:02:42.090 and of course the error is that the join method only works with strings and we want to allow 33 00:02:42.090 --> 00:02:46.660 numbers as well and that's why we are getting the error their so that's why we can't use join and 34 00:02:46.660 --> 00:02:54.450 if you change that to make a join then make...and run that 35 00:02:54.450 --> 00:02:59.650 you can see that clearly wasn't the result of what we were trying to to do their so what we're 36 00:02:59.650 --> 00:03:06.849 doing is undo those changes and stick to what we originally coded running that you can see it is 37 00:03:06.849 --> 00:03:09.959 working again but again to keep in the back in mind that this isn't the most 38 00:03:09.959 --> 00:03:13.650 efficient way of doing things and later in the course we're looking at list 39 00:03:13.650 --> 00:03:19.220 comprehension method to make this a little bit easier how you go about fixing it now the print function 40 00:03:19.220 --> 00:03:27.700 if you go back to that again so I just closed the window down so again that is control clicking if your on a 41 00:03:27.700 --> 00:03:32.569 Windows machine or Linux machine and on MAC it is command click 42 00:03:32.569 --> 00:03:37.420 go back to the definition again you can see there's also 3 named parameters this are also called 43 00:03:37.420 --> 00:03:41.609 default parameter values you can see the names the first one sep that is a string 44 00:03:41.609 --> 00:03:45.750 that's going default to a space if we don't provide a value and end is also a 45 00:03:45.750 --> 00:03:49.739 string the defaults to new line we have talked about that before but the final 46 00:03:49.739 --> 00:03:54.319 2 named parameters also add a printing to our file with an option to flush the file 47 00:03:54.319 --> 00:03:59.180 buffer so the data is written immediately at this point you're probably wondering why flush appears in 48 00:03:59.180 --> 00:04:02.750 the doc string but is missing in the actual function definition you can see 49 00:04:02.750 --> 00:04:08.840 how it is actually missing their so I'm wonder that as well and I don't have answer at the moment and Guido van Rossum who 50 00:04:08.840 --> 00:04:12.319 is the creator of Python was involved in a discussion about this in January 51 00:04:12.319 --> 00:04:17.250 2016 so we'll see what Python developers come up with it may be fixed by the time 52 00:04:17.250 --> 00:04:20.470 you view this video and in any event so what we can do now is we can 53 00:04:20.470 --> 00:04:24.300 make our center_text function behave in the same way as print 54 00:04:24.300 --> 00:04:28.870 by declaring the same named parameters then passing most of the arguments strait to 55 00:04:28.870 --> 00:04:34.030 the print function which is pretty cool so lets close this down and make a change to our 56 00:04:34.030 --> 00:04:41.379 center text we're gonna leave arguments their but we are gonna add these extra named parameters this named arguments again 57 00:04:41.379 --> 00:04:45.250 which are also known as the default parameter values so we're going to start with 58 00:04:45.250 --> 00:04:54.629 ....... 59 00:04:54.629 --> 00:05:01.310 ...... 60 00:05:01.310 --> 00:05:08.969 ...so we added those named arguments now as you can see and what we can now do is come down here and add a print on line 13 and add this on 61 00:05:08.969 --> 00:05:14.710 the end so we can put.... 62 00:05:14.710 --> 00:05:22.830 ....and noting when you type flush you can see that IntelliJ is 63 00:05:22.830 --> 00:05:26.990 telling you this is a parameters its actually showing you so it's quite interesting when you 64 00:05:26.990 --> 00:05:31.550 do you actually look at the way IntelliJ shows you a variable it actually does try 65 00:05:31.550 --> 00:05:34.930 and point out where things are parameters whether it's just a local 66 00:05:34.930 --> 00:05:42.740 variables so on and so forth so for example if we go back here we got v for variable as you can see 67 00:05:42.740 --> 00:05:48.569 that's pretty cool ok so....now because we can concatenating the values to print we don't pass 68 00:05:48.569 --> 00:05:53.310 sep on to the print function but instead we used in the separator and I'm gonna do that 69 00:05:53.310 --> 00:05:59.680 on line 11 so instead of putting a space their we are gonna put + sep...so whatever is 70 00:05:59.680 --> 00:06:02.900 passed as a separator is going to be added their and you can see the default if you don't 71 00:06:02.900 --> 00:06:07.039 specify a named argument is going to be a space and all the other named 72 00:06:07.039 --> 00:06:11.409 parameters or named arguments I've just provided to print when we called it so test this 73 00:06:11.409 --> 00:06:16.509 first by using a different separator when we go to call the code on line 22 so 74 00:06:16.509 --> 00:06:23.669 let's do that so we gonna put....and try running that and you can see that 75 00:06:23.669 --> 00:06:27.669 instead of using spaces to separate our arguments we've now got a colon so 76 00:06:27.669 --> 00:06:29.159 it's working perfectly 77 00:06:29.159 --> 00:06:33.110 although there is one problem you may have notice and that's the colon right at the end of the string 78 00:06:33.110 --> 00:06:37.670 we could write a code to strip it off and this is gonna be the final versions 79 00:06:37.670 --> 00:06:41.240 centered text now we would do that but we are gonna be using this function we cover list 80 00:06:41.240 --> 00:06:44.880 comprehensions later in the course and that will also cure that particular 81 00:06:44.880 --> 00:06:48.690 problems so if you wanna have a go at removing the trailing color right on the end 82 00:06:48.690 --> 00:06:53.580 using the techniques we've covered so far in the course by all means do that but we are not 83 00:06:53.580 --> 00:06:59.020 actually do that here now if the code on line 13 if that code 84 00:06:59.020 --> 00:07:04.640 end=end and file=file looks a little bit odd don't worry can 85 00:07:04.640 --> 00:07:07.630 take a bit of used to and if you've used other programming languages you might be 86 00:07:07.630 --> 00:07:11.170 wondering about things like variable scope and try to work and how Python is 87 00:07:11.170 --> 00:07:15.260 resolving the differences between the two ends but try to understand that in terms of 88 00:07:15.260 --> 00:07:18.900 scope doesn't really work so an easier way to look at it might be think of the 89 00:07:18.900 --> 00:07:25.450 parameter declaration end=/end which is defined on line 8 as doing 2 jobs 90 00:07:25.450 --> 00:07:30.110 so it specifies a parameter called end but it also changes the way the function 91 00:07:30.110 --> 00:07:34.080 is called so that whenever end= appears in the function call whatever 92 00:07:34.080 --> 00:07:39.210 follows the equal is assigned to the parameter end that it created so 93 00:07:39.210 --> 00:07:44.320 within the center_text there is also a variable called end that has a value / end 94 00:07:44.320 --> 00:07:49.550 unless another value was specified when center_text was called so essentially when line 13 95 00:07:49.550 --> 00:07:53.170 is executed end= is sort of interpreted as an instruction to take 96 00:07:53.170 --> 00:07:56.860 whatever comes next and use that value to initialize one of the prints 97 00:07:56.860 --> 00:08:00.920 parameters that also happens to be the end but that only exists within print 98 00:08:00.920 --> 00:08:05.220 if that makes sense and again what you can do here just to confirm if start typing 99 00:08:05.220 --> 00:08:09.920 it's always a good thing just to look at the little color coding circle to the 100 00:08:09.920 --> 00:08:10.940 left hand side 101 00:08:10.940 --> 00:08:13.990 IntelliJ is usually got your back there and that's clearly telling you that 102 00:08:13.990 --> 00:08:18.730 P is a perimeter and if you started typing texts as you can see 103 00:08:18.730 --> 00:08:22.740 we got a different code we got a V for variable so they are very handy to have that 104 00:08:22.740 --> 00:08:27.380 color-coded so that if your just wondering why a particular value and obviously we're 105 00:08:27.380 --> 00:08:32.510 talking about end so the same applies to end as well e you can see that is parameter of end so the only reason 106 00:08:32.510 --> 00:08:36.300 we've actually used the names end file and flush that we defined on line 8 107 00:08:36.300 --> 00:08:39.650 was to make the function compatible with print os if you change 108 00:08:39.650 --> 00:08:43.070 the definition it might make it easy to see what's going on so gonna do that two stages 109 00:08:43.070 --> 00:08:48.380 first let's go back to the definition on line 8 lets changes to instead of being end 110 00:08:48.380 --> 00:08:50.730 will put..... 111 00:08:50.730 --> 00:09:00.980 and for file....and for flush lets put....so now we have change the 112 00:09:00.980 --> 00:09:02.510 names you can see there now on line 113 00:09:02.510 --> 00:09:08.130 13 IntelliJ is really helpfully highlighted the errors and as such you can really 114 00:09:08.130 --> 00:09:12.190 easily see which bits of the call to print a which so the first bit is still 115 00:09:12.190 --> 00:09:17.060 the named arguments that's part of the print function and the second part is 116 00:09:17.060 --> 00:09:22.680 obviously the variable name where it is gonna get that value whatever the value is suppose to contain and if you hover over their we 117 00:09:22.680 --> 00:09:26.800 can see it says unresolved reference end and we can change that then once we do 118 00:09:26.800 --> 00:09:33.720 that any ambiguities removed so we type.... 119 00:09:33.720 --> 00:09:44.470 ...... 120 00:09:44.470 --> 00:09:51.050 the errors have gone away we should be up to run this and it's working well and again 121 00:09:51.050 --> 00:09:55.709 if you were in any doubt you can hover over when there's an error to see what the error 122 00:09:55.709 --> 00:10:02.709 was which I showed you just before I made the changes so now that that's done we have remove any ambiguity but at the expense 123 00:10:02.709 --> 00:10:06.279 of center_text now having a different signature to prints so obviously print had those 124 00:10:06.279 --> 00:10:11.019 names end file and flush but in our case the center_text functions 125 00:10:11.019 --> 00:10:11.959 is using 126 00:10:11.959 --> 00:10:17.190 end_char centered_text and flush_me so its got a different 127 00:10:17.190 --> 00:10:21.089 signature to prints so you actually hear that term signature by the way is a way 128 00:10:21.089 --> 00:10:26.130 of referring to a set of parameters in a function definitions so two functions 129 00:10:26.130 --> 00:10:30.290 that take exactly the same parameters are said to have the same signature 130 00:10:30.290 --> 00:10:35.050 so hopefully it is now clear which variables are being used and how we gave them 131 00:10:35.050 --> 00:10:39.190 different names and that's hopefully remove that early confusion so it would still be 132 00:10:39.190 --> 00:10:42.839 better to use the same names as print though because they will be the names that 133 00:10:42.839 --> 00:10:48.250 users of a functional likely be used to if they see a special print function because presumably 134 00:10:48.250 --> 00:10:52.819 that will be pretty used to standard printo function that comes with Python 135 00:10:52.819 --> 00:10:55.959 but if you find that confusing then of course you can call your parameters whatever you like 136 00:10:55.959 --> 00:10:59.130 and you saw as change that in this video so what I'm gonna do is put them back to what they 137 00:10:59.130 --> 00:11:03.120 were so that call to center_text is looks the same as to print 138 00:11:03.120 --> 00:11:19.860 so I'm going to rename this so what I'm gonna do is show you how to do a refactor so I'm gonna select the parameter and right click it and go refactor rename and change to end and press Refactor or press enter you can see how that has 139 00:11:19.860 --> 00:11:23.960 changed there and its also changed down here this can be a good way if your renaming variables 140 00:11:23.960 --> 00:11:28.380 to take the features of IntelliJ to do it for you and they will change 141 00:11:28.380 --> 00:11:31.700 all references to that wherever it's been used in your 142 00:11:31.700 --> 00:11:38.230 program so likewise for this one we change centered_file to just file press enter and change 143 00:11:38.230 --> 00:11:42.260 and likewise the third one flush and I can come down here if I wanted to 144 00:11:42.260 --> 00:11:48.500 instead refactor rename it still IntelliJ is smart enough to know its a parameter and if 145 00:11:48.500 --> 00:11:54.260 change that back to flush press enter its correctly change the parameters as well as the argument 146 00:11:54.260 --> 00:11:56.930 ok so I'm going to finish this video here now when we come back we're going 147 00:11:56.930 --> 00:12:01.860 to continue on and what will do first is change it so that the output goes to 148 00:12:01.860 --> 00:12:02.140 a file so see you in that next video