#100DaysOfCode in Python Transcripts
Chapter: Days 22-24: Decorators
Lecture: Function argument types: args and kwargs
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Alright, one thing we saw in the decorator
0:02
was the args and keyword args being passed in
0:06
and if you're new to python you might not be 100% aware
0:10
of all the different options you have to call a function.
0:14
So there's this great guide,
0:15
The Hitchhiker's Guide to Python
0:17
and it explains very well that you have positional,
0:21
keyword, and two arbitrary kind of arguments,
0:25
one is a list called *args and the other is a
0:28
keyword argument dictionary, which is **kwargs
0:32
and I just wanted to show a quick example
0:35
how this all works, so let's define a get profile function
0:41
and the only thing it's going to do is to
0:43
bounce the different kind of arguments
0:46
first let me just call it without anything
0:48
and it should error because name is a positional argument
0:52
which is required.
0:55
You see that doesn't work
0:56
and the error is pretty self explanatory
0:59
so let me then call it with a positional argument
1:04
and that worked, so the second type is keyword argument
1:07
which is not required and at set here to the default value
1:10
you can also set it to False, of course
1:12
saying active equals False, and that works
1:16
and then lets look at those two arbitrary kind of arguments
1:19
which is list and dictionary, so first
1:22
lets first do the arbitrary argument list
1:25
so that allows me to, for example, here we do sports
1:29
define one or more sports.
1:34
And I really like basketball.
1:38
Oops, yeah this is kind of strange that,
1:41
well it says positional argument follows keyword argument
1:45
so if you want to do it this way
1:47
you have make sure this not to be a keyword argument
1:50
so now it works. The reason is that the fourth
1:52
type of arguments is an arbitrary keyword dictionary
1:56
which goes towards the end. So let's pass in some words,
2:06
and they go last, right? So we here have the dictionary
2:10
of Pythonista and Topcoder, and that was the reason
2:12
we got the error before because the keyword arguments
2:15
always should go last, and to show arguments in action
2:19
I define this show args decorator that prints the args
2:23
before calling the function and prints the keyword arguments
2:27
after calling the function,
2:28
so just to put it into the context of the decorators
2:31
we are dealing with here. So we have show args
2:36
and I'm going to redefine the function, and this time
2:41
not to confuse it with the behavior of the decorator
2:45
I'm just going to print something else.
2:47
Hi from
2:50
the get profile
2:53
function
2:56
okay now lets see what happens
2:57
if I call and get profile again, now that it is decorated
3:01
of course I need to run the cell.
3:09
And here you see, so in the decorator it first
3:13
brings the args, then it does the actual function work
3:16
which is printing 'hi' from the get profile function
3:19
and then we're at the after stage of the decorator
3:23
printing the keyword arguments.
3:25
So here we see that the *args contains
3:27
the position arguments, keyword arguments, and the arbitrary
3:31
argument list, and the **kwargs contains then
3:36
the arbitrary keyword argument dictionary, so here you see
3:40
all those arguments actually being passed into the decorator
3:44
and hopefully now you have a better understanding what
3:47
*args and **kwargs means
3:50
and the different kind of ways
3:51
we can call function in python