1
00:00:05,320 --> 00:00:08,670
so in the previous we started
talking about functions and i ended it

2
00:00:08,670 --> 00:00:13,620
by saying that we could add the
functionality to our center

3
00:00:13,620 --> 00:00:19,810
text function and to do the
same as the example on line 22 with

4
00:00:19,810 --> 00:00:23,220
the 5 parameters that variable number of
parameters that are pass currently to the

5
00:00:23,220 --> 00:00:28,170
print function what we can do the way we
can do that is we can use the star

6
00:00:28,170 --> 00:00:31,000
talked about for arguments and then
iterate through the argument list and

7
00:00:31,000 --> 00:00:34,930
concatenate all the arguments into one string
before centering it so what we need to is

8
00:00:34,930 --> 00:00:38,020
make a change here we need to use the
Asterix and if you remembered by looking at

9
00:00:38,020 --> 00:00:43,270
the built-ins doc string for print we
knew that by adding an asterix that

10
00:00:43,270 --> 00:00:47,860
signifies Python their is as a variable
number of parameters so then what you need to do is

11
00:00:47,860 --> 00:00:51,050
we can't just put string equals text because we can now have multiple parameters

12
00:00:51,050 --> 00:00:56,890
so what we need to do is start off
by making an empty string and we need to

13
00:00:56,890 --> 00:01:04,300
create a for loop and actually add our
parameters so put.....

14
00:01:04,300 --> 00:01:12,110
and args we are gonna change this up here to be the
parameters so in this case the arguments and put.....

15
00:01:12,110 --> 00:01:20,560
...and once we do that of course

16
00:01:20,560 --> 00:01:25,920
the indents back on line 12 to the code
we're going to have a text variable

17
00:01:25,920 --> 00:01:29,540
which contains all the arguments and it
should work quite nicely without any

18
00:01:29,540 --> 00:01:34,110
code modification
in terms of calling the codes so if we run this we should find

19
00:01:34,110 --> 00:01:38,570
that it still works still works as it is
with one parameter but if we then changed

20
00:01:38,570 --> 00:01:47,420
this....and we do something like...

21
00:01:47,420 --> 00:01:53,650
...so we've got 5 paramteres and we've

22
00:01:53,650 --> 00:01:57,860
got a numbers and strings
we can use their so if we run that you will find in the

23
00:01:57,860 --> 00:02:02,720
last line first second 3 4 spam so it quite happily
accepted the arguments and centered them

24
00:02:02,720 --> 00:02:05,930
as you can see on the screen their now I have
said previously that concatenate strings

25
00:02:05,930 --> 00:02:09,650
in a list is not very efficient and
there is a more efficient way of doing

26
00:02:09,650 --> 00:02:13,200
this using list comprehensions but
we're not really at that point at the

27
00:02:13,200 --> 00:02:16,450
moment we're going to cover those later
in the course so we are not going to use one here

28
00:02:16,450 --> 00:02:20,170
just something to keep in mind that it
isn't the most efficient way

29
00:02:20,170 --> 00:02:23,900
currently of concatenating strings and
you might think ok maybe we can

30
00:02:23,900 --> 00:02:27,920
use the join here we can do something like....

31
00:02:27,920 --> 00:02:40,900
so you might think we can go........but if you run that we get an error

32
00:02:40,900 --> 00:02:45,450
and of course the error is that the join method only
works with strings and we want to allow

33
00:02:45,450 --> 00:02:50,020
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:50,020 --> 00:02:57,810
if you change that to make a join then
make...and run that

35
00:02:57,810 --> 00:03:03,010
you can see that clearly wasn't the result of what we
were trying to to do their so what we're

36
00:03:03,010 --> 00:03:10,200
doing is undo those changes and stick
to what we originally coded running that you can see it is

37
00:03:10,200 --> 00:03:13,310
working again but again to keep in the
back in mind that this isn't the most

38
00:03:13,310 --> 00:03:17,010
efficient way of doing things and later
in the course we're looking at list

39
00:03:17,010 --> 00:03:22,580
comprehension method to make this a
little bit easier how you go about fixing it now the print function

40
00:03:22,580 --> 00:03:31,060
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:31,060 --> 00:03:35,920
Windows machine or Linux machine and on MAC it is command click

42
00:03:35,920 --> 00:03:40,780
go back to the definition again you can see there's
also 3 named parameters this are also called

43
00:03:40,780 --> 00:03:44,960
default parameter values you can see the
names the first one sep that is a string

44
00:03:44,960 --> 00:03:49,110
that's going default to a space if we
don't provide a value and end is also a

45
00:03:49,110 --> 00:03:53,090
string the defaults to new line we have
talked about that before but the final

46
00:03:53,090 --> 00:03:57,670
2 named parameters also add a printing to
our file with an option to flush the file

47
00:03:57,670 --> 00:04:02,540
buffer so the data is written immediately at this point you're
probably wondering why flush appears in

48
00:04:02,540 --> 00:04:06,110
the doc string but is missing in the
actual function definition you can see

49
00:04:06,110 --> 00:04:12,200
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:12,200 --> 00:04:15,670
is the creator of Python was involved
in a discussion about this in January

51
00:04:15,670 --> 00:04:20,610
2016 so we'll see what Python developers
come up with it may be fixed by the time

52
00:04:20,610 --> 00:04:23,830
you view this video and in any
event so what we can do now is we can

53
00:04:23,830 --> 00:04:27,660
make our center_text function behave
in the same way as print

54
00:04:27,660 --> 00:04:32,230
by declaring the same named parameters
then passing most of the arguments strait to

55
00:04:32,230 --> 00:04:37,390
the print function which is pretty cool so lets close this down and make a change to our

56
00:04:37,390 --> 00:04:44,730
center text we're gonna leave arguments their but we are gonna add these extra named parameters this named arguments again

57
00:04:44,730 --> 00:04:48,610
which are also known as the default parameter values so we're going to start with

58
00:04:48,610 --> 00:04:57,980
.......

59
00:04:57,980 --> 00:05:04,670
......

60
00:05:04,670 --> 00:05:12,320
...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:12,320 --> 00:05:18,070
the end so we can put....

62
00:05:18,070 --> 00:05:26,190
....and noting when you type flush you can see that IntelliJ is

63
00:05:26,190 --> 00:05:30,350
telling you this is a parameters its actually showing you so it's quite interesting when you

64
00:05:30,350 --> 00:05:34,910
do you actually look at the way IntelliJ
shows you a variable it actually does try

65
00:05:34,910 --> 00:05:38,290
and point out where things are
parameters whether it's just a local

66
00:05:38,290 --> 00:05:46,100
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:46,100 --> 00:05:51,920
that's pretty cool ok so....now because we can concatenating the values to print we don't pass

68
00:05:51,920 --> 00:05:56,670
sep on to the print function but
instead we used in the separator and I'm gonna do that

69
00:05:56,670 --> 00:06:03,040
on line 11 so instead of putting a space their we are gonna put + sep...so whatever is

70
00:06:03,040 --> 00:06:06,260
passed as a separator is going to be
added their and you can see the default if you don't

71
00:06:06,260 --> 00:06:10,390
specify a named argument is going to be a
space and all the other named

72
00:06:10,390 --> 00:06:14,760
parameters or named arguments I've just provided
to print when we called it so test this

73
00:06:14,760 --> 00:06:19,860
first by using a different separator when
we go to call the code on line 22 so

74
00:06:19,860 --> 00:06:27,020
let's do that so we gonna put....and try running that and you can see that

75
00:06:27,020 --> 00:06:31,020
instead of using spaces to separate our
arguments we've now got a colon so

76
00:06:31,020 --> 00:06:32,510
it's working perfectly

77
00:06:32,510 --> 00:06:36,470
although there is one problem you may have notice and that's
the colon right at the end of the string

78
00:06:36,470 --> 00:06:41,030
we could write a code to strip it off
and this is gonna be the final versions

79
00:06:41,030 --> 00:06:44,600
centered text now we would do that but we are gonna be using this function we cover list

80
00:06:44,600 --> 00:06:48,240
comprehensions later in the course and
that will also cure that particular

81
00:06:48,240 --> 00:06:52,050
problems so if you wanna have a go at
removing the trailing color right on the end

82
00:06:52,050 --> 00:06:56,940
using the techniques we've covered so far in
the course by all means do that but we are not

83
00:06:56,940 --> 00:07:02,380
actually do that here now if the code
on line 13 if that code

84
00:07:02,380 --> 00:07:08,000
end=end and file=file looks a little bit odd don't worry can

85
00:07:08,000 --> 00:07:10,990
take a bit of used to and if you've used other programming languages you might be

86
00:07:10,990 --> 00:07:14,530
wondering about things like variable scope
and try to work and how Python is

87
00:07:14,530 --> 00:07:18,620
resolving the differences between the
two ends but try to understand that in terms of

88
00:07:18,620 --> 00:07:22,260
scope doesn't really work so an easier way
to look at it might be think of the

89
00:07:22,260 --> 00:07:28,810
parameter declaration end=/end which is defined on line 8 as doing 2 jobs

90
00:07:28,810 --> 00:07:33,470
so it specifies a parameter called end but it also changes the way the function

91
00:07:33,470 --> 00:07:37,440
is called so that whenever end= appears in the function call whatever

92
00:07:37,440 --> 00:07:42,570
follows the equal is assigned to the
parameter end that it created so

93
00:07:42,570 --> 00:07:47,680
within the center_text there is also a
variable called end that has a value / end

94
00:07:47,680 --> 00:07:52,910
unless another value was specified when
center_text was called so essentially when line 13

95
00:07:52,910 --> 00:07:56,530
is executed end= is sort of
interpreted as an instruction to take

96
00:07:56,530 --> 00:08:00,220
whatever comes next and use that value
to initialize one of the prints

97
00:08:00,220 --> 00:08:04,280
parameters that also happens to be the
end but that only exists within print

98
00:08:04,280 --> 00:08:08,580
if that makes sense and again what you can do here just to confirm if start typing

99
00:08:08,580 --> 00:08:13,280
it's always a good thing just to look at
the little color coding circle to the

100
00:08:13,280 --> 00:08:14,300
left hand side

101
00:08:14,300 --> 00:08:17,350
IntelliJ is usually got your back there
and that's clearly telling you that

102
00:08:17,350 --> 00:08:22,090
P is a perimeter and if you started typing
texts as you can see

103
00:08:22,090 --> 00:08:26,100
we got a different code we got a V for variable so they are very handy to have that

104
00:08:26,100 --> 00:08:30,740
color-coded so that if your just wondering why a
particular value and obviously we're

105
00:08:30,740 --> 00:08:35,870
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:35,870 --> 00:08:39,659
we've actually used the names end file and flush that we defined on line 8

107
00:08:39,659 --> 00:08:43,010
was to make the function
compatible with print os if you change

108
00:08:43,010 --> 00:08:46,430
the definition it might make it easy to see what's
going on so gonna do that two stages

109
00:08:46,430 --> 00:08:51,740
first let's go back to the definition
on line 8 lets changes to instead of being end

110
00:08:51,740 --> 00:08:54,090
will put.....

111
00:08:54,090 --> 00:09:04,340
and for file....and for flush lets put....so now we have change the

112
00:09:04,340 --> 00:09:05,870
names you can see there now on line

113
00:09:05,870 --> 00:09:11,490
13 IntelliJ is really helpfully
highlighted the errors and as such you can really

114
00:09:11,490 --> 00:09:15,550
easily see which bits of the call to
print a which so the first bit is still

115
00:09:15,550 --> 00:09:20,420
the named arguments that's part of the print
function and the second part is

116
00:09:20,420 --> 00:09:26,040
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:26,040 --> 00:09:30,160
can see it says unresolved reference end
and we can change that then once we do

118
00:09:30,160 --> 00:09:37,080
that any ambiguities removed so we
type....

119
00:09:37,080 --> 00:09:47,830
......

120
00:09:47,830 --> 00:09:54,410
the errors have gone away we should be up to
run this and it's working well and again

121
00:09:54,410 --> 00:09:59,060
if you were in any doubt you can hover over when
there's an error to see what the error

122
00:09:59,060 --> 00:10:06,060
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:06,060 --> 00:10:09,630
of center_text now having a different
signature to prints so obviously print had those

124
00:10:09,630 --> 00:10:14,370
names end file and flush but in
our case the center_text functions

125
00:10:14,370 --> 00:10:15,310
is using

126
00:10:15,310 --> 00:10:20,550
end_char centered_text and flush_me so its got a different

127
00:10:20,550 --> 00:10:24,440
signature to prints so you actually hear
that term signature by the way is a way

128
00:10:24,440 --> 00:10:29,490
of referring to a set of parameters in a
function definitions so two functions

129
00:10:29,490 --> 00:10:33,650
that take exactly the same parameters
are said to have the same signature

130
00:10:33,650 --> 00:10:38,410
so hopefully it is now clear which variables are
being used and how we gave them

131
00:10:38,410 --> 00:10:42,550
different names and that's hopefully remove
that early confusion so it would still be

132
00:10:42,550 --> 00:10:46,190
better to use the same names as print
though because they will be the names that

133
00:10:46,190 --> 00:10:51,610
users of a functional likely be used to
if they see a special print function because presumably

134
00:10:51,610 --> 00:10:56,170
that will be pretty used to standard printo
function that comes with Python

135
00:10:56,170 --> 00:10:59,310
but if you find that confusing then of course you can
call your parameters whatever you like

136
00:10:59,310 --> 00:11:02,490
and you saw as change that in this video so what I'm gonna do is put them back to what they

137
00:11:02,490 --> 00:11:06,480
were so that call to center_text is looks the same
as to print

138
00:11:06,480 --> 00:11:23,220
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:23,220 --> 00:11:27,320
changed there and its also changed down here this can
be a good way if your renaming variables

140
00:11:27,320 --> 00:11:31,740
to take the features
of IntelliJ to do it for you and they will change

141
00:11:31,740 --> 00:11:35,060
all references to that wherever it's
been used in your

142
00:11:35,060 --> 00:11:41,590
program so likewise for this one we change centered_file to just file press enter and change

143
00:11:41,590 --> 00:11:45,620
and likewise the third one
flush and I can come down here if I wanted to

144
00:11:45,620 --> 00:11:51,860
instead refactor rename it still
IntelliJ is smart enough to know its a parameter and if

145
00:11:51,860 --> 00:11:57,620
change that back to flush press enter its correctly
change the parameters as well as the argument

146
00:11:57,620 --> 00:12:00,290
ok so I'm going to finish this video
here now when we come back we're going

147
00:12:00,290 --> 00:12:05,220
to continue on and what will do first is
change it so that the output goes to

148
00:12:05,220 --> 00:12:05,500
a file so see you in that next video

