1
00:00:00,000 --> 00:00:05,990
Sometimes it's convenient to be able to pass named parameters into a function,

2
00:00:06,000 --> 00:00:07,990
and there is facility for that in Python.

3
00:00:08,000 --> 00:00:17,990
Go head and make a working copy of functions.py and we'll call it functions-working.py

4
00:00:18,000 --> 00:00:21,990
and open that working copy and here we have our little test function.

5
00:00:22,000 --> 00:00:27,990
Sometimes you might want to pass parameters to it and have them look like this.

6
00:00:28,000 --> 00:00:36,990
one=1, two=2, four=42, and so here you are actually passing named

7
00:00:37,000 --> 00:00:41,990
arguments and the caller is naming them rather than the receiver.

8
00:00:42,000 --> 00:00:44,990
So these arguments are not named on the receiving end.

9
00:00:45,000 --> 00:00:52,990
So these are specified with the two asterisks and very commonly called kwargs

10
00:00:53,000 --> 00:00:58,990
for keyword args, keyword arguments, and these are accessed like this.

11
00:00:59,000 --> 00:01:08,990
kwargs is actually a dictionary and so I can say kwargs sub 'one', like that, and

12
00:01:09,000 --> 00:01:21,990
kwargs sub 'two', kwargs sub 'four' and when I save these and run it, you see that we are

13
00:01:22,000 --> 00:01:25,990
getting those values here, 1, 2, and 42.

14
00:01:26,000 --> 00:01:31,990
These keyword arguments can be combined with normal positional arguments, so you

15
00:01:32,000 --> 00:01:42,990
can pass it 5, 6, 7, 8, 9, and 10 and these can be named arguments.

16
00:01:43,000 --> 00:01:49,990
this, that and other and they can even be tuple arguments.

17
00:01:50,000 --> 00:01:54,990
And so what this will do is this will pass the named arguments, these first

18
00:01:55,000 --> 00:02:00,990
three will be this, that, and other, and then these here will be in this tuple

19
00:02:01,000 --> 00:02:02,990
and then you have your named arguments.

20
00:02:03,000 --> 00:02:07,990
The only restriction is that they must actually be specified in this order.

21
00:02:08,000 --> 00:02:12,990
Your named arguments first, your arbitrary tuple arguments after that, and your

22
00:02:13,000 --> 00:02:14,990
keyword argument after that.

23
00:02:15,000 --> 00:02:19,990
Other than that, there is no restriction on the number or even the type of

24
00:02:20,000 --> 00:02:21,990
all these arguments.

25
00:02:22,000 --> 00:02:27,990
So go ahead and take a look at all of those.

26
00:02:28,000 --> 00:02:33,990
We have this, that, and other and we will go ahead and look at the tuple all at

27
00:02:34,000 --> 00:02:38,990
once like that and we will save this and run it.

28
00:02:39,000 --> 00:02:43,990
You see we have them orders 5, 6, 7, there is our tuple, and there is

29
00:02:44,000 --> 00:02:45,990
our keyword arguments.

30
00:02:46,000 --> 00:02:50,990
Now, the keywords arguments, of course, are optional and the names of the

31
00:02:51,000 --> 00:02:56,990
keywords are not necessarily known by the receiver, just like with the tuple.

32
00:02:57,000 --> 00:03:02,990
Let's go head and get rid of all of this and we will look at the

33
00:03:03,000 --> 00:03:09,990
keyword arguments first. For k in kwargs:

34
00:03:10,000 --> 00:03:14,990
print k and kwargsk sub k.

35
00:03:15,000 --> 00:03:17,990
So this will print one per line each of the keyword arguments.

36
00:03:18,000 --> 00:03:19,990
I save this and run it.

37
00:03:20,000 --> 00:03:24,990
4, 2, 1 just like with any dictionary.

38
00:03:25,000 --> 00:03:36,990
So if I change these up here, 3 and I have 17 and in fact you see we still have them.

39
00:03:37,000 --> 00:03:40,990
Of course, because it is a dictionary, it's going to come out in no particular order.

40
00:03:41,000 --> 00:03:44,990
But more often than not, you are going to use these keyword arguments

41
00:03:45,000 --> 00:03:48,990
for settings and flags and things like that and you'll test for them in your

42
00:03:49,000 --> 00:03:51,990
function, and you are not going to really be counting on the order in which

43
00:03:52,000 --> 00:03:52,990
they are presented.

44
00:03:53,000 --> 00:03:57,990
On the other hand, the tuple arguments will be presented in the order that they

45
00:03:58,000 --> 00:04:01,990
are passed because they are being passed as a tuple.

46
00:04:02,000 --> 00:04:05,990
So if I say, for n in args:

47
00:04:06,000 --> 00:04:13,990
print(n), and we'll save that and run it, then we get those actually in the order

48
00:04:14,000 --> 00:04:15,990
that they were passed.

49
00:04:16,000 --> 00:04:18,990
So that's how you can pass named arguments to a function.

50
00:04:19,000 --> 00:04:24,990
This is very commonly used for settings and flags and things like that and also,

51
00:04:25,000 --> 00:04:28,990
this is how you can combine them with the arbitrary tuple arguments and with

52
00:04:29,000 --> 00:04:39,000
your normal positional arguments.

