1
00:00:00,000 --> 00:00:05,990
Sometimes when you're defining a function, you might need an arbitrary number of arguments.

2
00:00:06,000 --> 00:00:09,990
Arguments that aren't necessarily going to be used every time and aren't

3
00:00:10,000 --> 00:00:14,990
necessarily going to even be named, perhaps a list of things.

4
00:00:15,000 --> 00:00:18,990
Python has a facility for this and let's see how that works.

5
00:00:19,000 --> 00:00:21,990
We'll go ahead and make a working copy of functions.py, call it

6
00:00:22,000 --> 00:00:29,990
functions-working.py and we'll open that working copy up and here we have our

7
00:00:30,000 --> 00:00:31,990
little test function.

8
00:00:32,000 --> 00:00:37,990
And let's say that we just want to give it an arbitrary list of arguments and so

9
00:00:38,000 --> 00:00:40,990
that's done simply like that.

10
00:00:41,000 --> 00:00:46,990
The asterisk is special in this place as it means that this is just a list

11
00:00:47,000 --> 00:00:48,990
of optional arguments.

12
00:00:49,000 --> 00:00:52,990
You can have before it-- you can have a named argument or several of them.

13
00:00:53,000 --> 00:01:00,990
you could have this, that and the other, and then after all of that you can have

14
00:01:01,000 --> 00:01:05,990
your optional arguments, and so when you call it you might need to give it some

15
00:01:06,000 --> 00:01:12,990
actual values there and then you could have your arbitrary number of optional

16
00:01:13,000 --> 00:01:14,990
arguments like that.

17
00:01:15,000 --> 00:01:20,990
So if we want to list those, we can just get rid of this text here and say

18
00:01:21,000 --> 00:01:27,990
this, that, and other, and if we save that and run it, you'll notice that we get

19
00:01:28,000 --> 00:01:29,990
the 1, 2 and the 3.

20
00:01:30,000 --> 00:01:31,990
What about these other ones?

21
00:01:32,000 --> 00:01:36,990
If I say args, what I'll get is I'll get a tuple with those values.

22
00:01:37,000 --> 00:01:39,990
Save that and run it and there is our tuple.

23
00:01:40,000 --> 00:01:46,990
So you can see that it's just a normal tuple, as in Python, and so I can use it

24
00:01:47,000 --> 00:01:56,990
as an Iterator if I want to. I can say for n in args like that, print n, end,

25
00:01:57,000 --> 00:02:04,990
equals blank or let's give it a space like that. And I can save it and run it

26
00:02:05,000 --> 00:02:10,990
and now we have our arbitrary arguments after our named arguments.

27
00:02:11,000 --> 00:02:13,990
So, it's good to keep in mind that this is a tuple.

28
00:02:14,000 --> 00:02:19,990
That means that it is immutable, can't add to it or change anything in it, but

29
00:02:20,000 --> 00:02:30,000
it is an excellent way to get an arbitrary list of arguments into a function.

