#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 was the args and keyword args being passed in and if you're new to Python you might not be 100% aware
0:11 of all the different options you have to call a function. So there's this great guide, The Hitchhiker's Guide to Python
0:18 and it explains very well that you have positional, keyword, and two arbitrary kind of arguments, one is a list called *args and the other is a
0:29 keyword argument dictionary, which is **kwargs and I just wanted to show a quick example how this all works, so let's define a get profile function
0:42 and the only thing it's going to do is to bounce the different kind of arguments first let me just call it without anything
0:49 and it should error because name is a positional argument which is required. You see that doesn't work and the error is pretty self explanatory
1:00 so let me then call it with a positional argument and that worked, so the second type is keyword argument
1:08 which is not required and at set here to the default value you can also set it to False, of course saying active equals False, and that works
1:17 and then lets look at those two arbitrary kind of arguments which is list and dictionary, so first lets first do the arbitrary argument list
1:26 so that allows me to, for example, here we do sports define one or more sports. And I really like basketball. Oops, yeah this is kind of strange that,
1:42 well it says positional argument follows keyword argument so if you want to do it this way you have make sure this not to be a keyword argument
1:51 so now it works. The reason is that the fourth type of arguments is an arbitrary keyword dictionary
1:57 which goes towards the end. So let's pass in some words, and they go last, right? So we here have the dictionary
2:11 of Pythonista and Topcoder, and that was the reason we got the error before because the keyword arguments
2:16 always should go last, and to show arguments in action I define this show args decorator that prints the args
2:24 before calling the function and prints the keyword arguments after calling the function, so just to put it into the context of the decorators
2:32 we are dealing with here. So we have show args and I'm going to redefine the function, and this time
2:42 not to confuse it with the behavior of the decorator I'm just going to print something else. Hi from the get profile function
2:57 okay now lets see what happens if I call and get profile again, now that it is decorated of course I need to run the cell.
3:10 And here you see, so in the decorator it first brings the args, then it does the actual function work
3:17 which is printing 'hi' from the get profile function and then we're at the after stage of the decorator printing the keyword arguments.
3:26 So here we see that the *args contains the position arguments, keyword arguments, and the arbitrary argument list, and the **kwargs contains then
3:37 the arbitrary keyword argument dictionary, so here you see all those arguments actually being passed into the decorator
3:45 and hopefully now you have a better understanding what *args and **kwargs means and the different kind of ways we can call function in Python

Talk Python's Mastodon Michael Kennedy's Mastodon