1
00:00:05,280 --> 00:00:09,920
The last things I want to cover,
in this section on functions, is *args.

2
00:00:09,920 --> 00:00:11,519
We first saw that when we looked at the

3
00:00:11,519 --> 00:00:14,960
function signature for print.
So call up the help for print by

4
00:00:14,960 --> 00:00:18,320
clicking on it at the end of our
functions.py file

5
00:00:18,320 --> 00:00:21,439
and use Ctrl-Q, or you can just pause

6
00:00:21,440 --> 00:00:25,740
as I did, and it came up automatically.
Remember, that's Ctrl-J on a Mac.

7
00:00:25,740 --> 00:00:28,240
I won't mention that again -
you've got the message by now.

8
00:00:28,240 --> 00:00:33,680
So the exact signature can vary slightly
with different versions of Python.

9
00:00:33,680 --> 00:00:37,960
So I've got *values, as you
can see there, as the first parameter

10
00:00:37,960 --> 00:00:40,239
but you may have *objects instead.

11
00:00:40,239 --> 00:00:43,120
The exact names of parameters
aren't important

12
00:00:43,120 --> 00:00:46,719
unless they're keyword arguments.
In this video, we'll see why

13
00:00:46,720 --> 00:00:52,800
there's an asterisk before values. The 
most usual place to find * something

14
00:00:52,800 --> 00:00:57,280
is in a function definition,
but they can be useful in other places.

15
00:00:57,280 --> 00:01:01,520
I'm going to start by describing what
the * does. Once we've seen that,

16
00:01:01,520 --> 00:01:05,680
we'll see how it lets you provide
multiple arguments to a function.

17
00:01:05,680 --> 00:01:17,140
So let's create a new Python file. I'm
going to call mine star_examples.

18
00:01:17,140 --> 00:01:21,280
When you use an asterisk before an
object, it unpacks the sequence.

19
00:01:21,280 --> 00:01:25,200
So I'm going to type numbers is equal to,
and then in parentheses,

20
00:01:25,200 --> 00:01:28,320
0 comma 1 comma 2 comma 3 comma

21
00:01:28,320 --> 00:01:31,840
4 comma 5. We've got our closing
parentheses there,

22
00:01:31,840 --> 00:01:37,200
and on line 3 we're gonna print
parentheses numbers

23
00:01:38,400 --> 00:01:43,840
So we've got a tuple called numbers, and when
we run the program,

24
00:01:43,840 --> 00:01:47,360
you can see that's what we get printed
out. So let's add a line -

25
00:01:47,360 --> 00:01:57,120
line 4 - and I'm actually going to type
print parentheses *numbers.

26
00:01:57,120 --> 00:02:01,680
So we're printing out *numbers now, so
let's run the program.

27
00:02:02,479 --> 00:02:07,040
Okay, so what happened there? Well
*numbers was unpacked,

28
00:02:07,040 --> 00:02:10,639
then the unpacked values were
passed to the print function.

29
00:02:10,639 --> 00:02:13,840
So in other words, what the print
function really got, was this:

30
00:02:13,840 --> 00:02:17,680
I'm going to add some code here,
print parentheses

31
00:02:17,680 --> 00:02:22,239
0 comma 1 comma 2 comma 3 comma 4 comma 5.

32
00:02:22,239 --> 00:02:25,760
That's effectively, what the print
function actually received, or got.

33
00:02:25,760 --> 00:02:31,280
So run the program now, and obviously,
we'll get exactly the same result.

34
00:02:31,280 --> 00:02:36,239
Alright, just to make this clearer, though,
let's add a separator to all three lines.

35
00:02:36,239 --> 00:02:39,519
So starting on line number one, put a
comma space

36
00:02:39,519 --> 00:02:42,879
sep equals and a semicolon in double quotes,

37
00:02:42,879 --> 00:02:47,120
and I'll just take a copy of that line
and we'll paste exactly the same thing

38
00:02:47,120 --> 00:02:53,440
to the next two lines. So
line four, after numbers, then after the

39
00:02:53,440 --> 00:03:00,239
number five,
on line five. So if we run this now,

40
00:03:01,280 --> 00:03:05,519
and you can see we get the values
separated by a semicolon.

41
00:03:05,519 --> 00:03:09,120
Notice that there's no semicolon in the
first line of output.

42
00:03:09,120 --> 00:03:14,040
We're only printing a single value there,
so the separator character isn't used.

43
00:03:14,040 --> 00:03:17,120
So that should demonstrate
that *numbers was unpacked,

44
00:03:17,120 --> 00:03:21,200
and the individual values were passed to
the print function.

45
00:03:21,200 --> 00:03:24,400
You can use * to unpack any sequence type.

46
00:03:24,400 --> 00:03:28,319
It's very useful sometimes, but it isn't
something you'll do very often.

47
00:03:28,319 --> 00:03:32,239
In fact, you may never do it. But where it
gets incredibly useful,

48
00:03:32,239 --> 00:03:36,720
is when we use it the other way around.
Instead of unpacking the sequence that

49
00:03:36,720 --> 00:03:39,680
we pass as an argument, we can specify that a

50
00:03:39,680 --> 00:03:43,840
parameter is an unpacked sequence. So the easiest

51
00:03:43,840 --> 00:03:47,280
way to demonstrate that
is with a bit of code. What I'm going to

52
00:03:47,280 --> 00:03:51,599
do is start by commenting out this
printing code,

53
00:03:51,600 --> 00:03:55,840
and I'm going to add a function
definition, so let's go ahead and do that.

54
00:03:55,840 --> 00:04:00,319
Alright, so down here, def test_star

55
00:04:00,320 --> 00:04:07,560
parentheses *args colon.

56
00:04:07,560 --> 00:04:13,620
Then we're going to,
on line nine, print parentheses args,

57
00:04:13,620 --> 00:04:20,959
line 10, for x in args colon,
print x in parentheses.

58
00:04:20,959 --> 00:04:28,560
Okay, and then down here, line 14,
test_star parentheses

59
00:04:28,560 --> 00:04:34,480
zero comma one comma two comma three
comma four comma five.

60
00:04:34,480 --> 00:04:37,840
Alright, so run the program now,

61
00:04:38,479 --> 00:04:43,840
and you can see what it's produced there.
Inside our function,

62
00:04:44,320 --> 00:04:48,639
args is a tuple. We can see that on the
first line of the output.

63
00:04:48,639 --> 00:04:52,720
The function then iterates over args and
prints each of its items.

64
00:04:52,720 --> 00:04:56,000
So on line eight in our function
definition, the *

65
00:04:56,000 --> 00:05:00,400
means that this parameter will be
replaced by an unpacked tuple.

66
00:05:00,400 --> 00:05:04,240
When the function's called, the values
will be packed up into a tuple,

67
00:05:04,240 --> 00:05:08,560
and we can see that on line 14 when we
call the function.

68
00:05:08,560 --> 00:05:12,080
We passed a total of six arguments - the
numbers zero to five -

69
00:05:12,080 --> 00:05:16,000
and they get packed up into the tuple
called args. Using *args means that

70
00:05:16,000 --> 00:05:19,199
we can provide a variable number of
arguments to our functions.

71
00:05:19,199 --> 00:05:23,759
On line 14, we pass six arguments. We
can pass more than that, or even

72
00:05:23,759 --> 00:05:27,759
no arguments. So to demonstrate that, let's
add some code.

73
00:05:27,760 --> 00:05:32,560
So down here, and here I'll do a print, left
 and right parentheses, it's an empty line,

74
00:05:32,560 --> 00:05:35,740
and on the next line, test_star

75
00:05:35,740 --> 00:05:39,200
parentheses, but with nothing in the parentheses.

76
00:05:39,200 --> 00:05:41,840
So on line 16, I'm not providing any

77
00:05:41,840 --> 00:05:44,960
arguments to the print function.
print knows to just print a blank line

78
00:05:44,960 --> 00:05:49,120
when we do that. In fact, all it does
is print the default value for end,

79
00:05:49,120 --> 00:05:52,960
which is a new line.
On line 17, we call our test_star

80
00:05:52,960 --> 00:05:57,440
function with no arguments.
If we run this,

81
00:05:58,160 --> 00:06:01,919
we get an empty tuple printed out, as the
last line of the output.

82
00:06:01,919 --> 00:06:06,080
Importantly, our function didn't crash. So
we now have a way to write

83
00:06:06,080 --> 00:06:11,660
functions that can take 0 or more
arguments, just like the built-in print function.

84
00:06:11,660 --> 00:06:15,540
Before we look at a few rules, I'll
quickly review what we've just done.

85
00:06:15,540 --> 00:06:18,160
So looking at our code, up there on the

86
00:06:18,160 --> 00:06:22,400
now commented out line four,
we passed *numbers

87
00:06:22,400 --> 00:06:25,680
to the print function. So that
caused the tuple to be unpacked,

88
00:06:25,680 --> 00:06:30,100
and the print function really got the 6
arguments that we can see on line 5.

89
00:06:30,100 --> 00:06:32,080
As we saw, both those lines printed out

90
00:06:32,080 --> 00:06:35,600
exactly the same thing.
In our test_star function,

91
00:06:35,600 --> 00:06:39,680
we did the opposite. We used
*args as the parameter,

92
00:06:39,680 --> 00:06:43,120
which means that it represents
an unpacked tuple.

93
00:06:43,120 --> 00:06:49,680
On line 14, the values 0 1 2 3
4 and 5 are treated as an unpacked tuple.

94
00:06:49,680 --> 00:06:53,599
Python packs them up for us. The *
before the parameter name,

95
00:06:53,599 --> 00:06:56,880
on line 8, instructs Python to do just that.

96
00:06:56,880 --> 00:07:00,720
If *args represents an unpacked
tuple, then args

97
00:07:00,720 --> 00:07:06,200
without the *, is a tuple.
The output from line 9 confirmed that.

98
00:07:06,200 --> 00:07:10,560
When we printed args,
we got a tuple in the output,

99
00:07:11,280 --> 00:07:14,800
and that's really all there is to it.

100
00:07:15,360 --> 00:07:18,560
When you put an asterisk,
or star before a parameter name,

101
00:07:18,560 --> 00:07:23,360
Python follows a few simple rules and
packs the values up into a tuple.

102
00:07:23,360 --> 00:07:26,479
You can then use that tuple,
in your function's code

103
00:07:26,479 --> 00:07:30,720
to use however many arguments were
provided. Another example will help make

104
00:07:30,720 --> 00:07:33,520
that clearer,
so in the next video, we'll make a change

105
00:07:33,520 --> 00:07:40,000
to our color_print function.
See you in the next video.

