Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes it's convenient to be able to pass named parameters into a function, and there is facility for that in Python. Go head and make a working copy of functions.py and we'll call it functions-working.py and open that working copy and here we have our little test function. Sometimes you might want to pass parameters to it and have them look like this. one=1, two=2, four=42, and so here you are actually passing named arguments and the caller is naming them rather than the receiver.
So these arguments are not named on the receiving end. So these are specified with the two asterisks and very commonly called kwargs for keyword args, keyword arguments, and these are accessed like this. kwargs is actually a dictionary and so I can say kwargs sub 'one', like that, and kwargs sub 'two', kwargs sub 'four' and when I save these and run it, you see that we are getting those values here, 1, 2, and 42.
These keyword arguments can be combined with normal positional arguments, so you can pass it 5, 6, 7, 8, 9, and 10 and these can be named arguments. this, that and other and they can even be tuple arguments. And so what this will do is this will pass the named arguments, these first three will be this, that, and other, and then these here will be in this tuple and then you have your named arguments.
The only restriction is that they must actually be specified in this order. Your named arguments first, your arbitrary tuple arguments after that, and your keyword argument after that. Other than that, there is no restriction on the number or even the type of all these arguments. So go ahead and take a look at all of those. We have this, that, and other and we will go ahead and look at the tuple all at once like that and we will save this and run it.
You see we have them orders 5, 6, 7, there is our tuple, and there is our keyword arguments. Now, the keywords arguments, of course, are optional and the names of the keywords are not necessarily known by the receiver, just like with the tuple. Let's go head and get rid of all of this and we will look at the keyword arguments first. For k in kwargs: print k and kwargsk sub k.
So this will print one per line each of the keyword arguments. I save this and run it. 4, 2, 1 just like with any dictionary. So if I change these up here, 3 and I have 17 and in fact you see we still have them. Of course, because it is a dictionary, it's going to come out in no particular order. But more often than not, you are going to use these keyword arguments for settings and flags and things like that and you'll test for them in your function, and you are not going to really be counting on the order in which they are presented.
On the other hand, the tuple arguments will be presented in the order that they are passed because they are being passed as a tuple. So if I say, for n in args: print(n), and we'll save that and run it, then we get those actually in the order that they were passed. So that's how you can pass named arguments to a function. This is very commonly used for settings and flags and things like that and also, this is how you can combine them with the arbitrary tuple arguments and with your normal positional arguments.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104377 Viewers
56 Video lessons · 116282 Viewers
71 Video lessons · 85527 Viewers
131 Video lessons · 40945 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.