Speaker 1: Let's talk about something called
arguments and keyword arguments.
I mean, we've already seen that before, right?
With a function we actually have the special characters that
we can use called args and star star keyword args.
How can we use these?
Well, let's have a look.
Let's try and have a function, let's say define,
and we'll call this a super_func, for super function,
that receives some sort of arguments.
And these arguments that we're gonna receive,
and remember, although I'm calling these args,
this itself is a parameter, remember?
But let's say that we wanna just return
sum of the arguments.
And you might be wondering, wait, whoa, sum?
Don't we have to define that function?
Well, sum actually exists in Python,
as you can see over here.
So we can just return the sum of the arguments.
So let's try this.
Let's say super_func and give it arguments (1,2,3,4,5).
If I click run,
hmm, I get an error.
Super_func takes one positional argument,
but five were given.
And that makes sense, right?
Like I only have one parameter here,
which is a positional argument that it accepts.
And I'm trying to sum this,
so it only receives one but I'm giving it
to all these things that it doesn't know about.
And this is where we can use something like this
by adding a star to here.
We're saying hey, this can accept any number
of positional arguments like this.
As many as I want.
As a matter of fact, let's print this out.
If I do print(*args) and we run this function,
we see that the print(*args)
gives us one, two, three, four, five.
These are all the parameters or the arguments that we get.
And if I actually remove the star here and click run,
look at that.
I actually get this as a tuple.
So args inside of this function is a tuple of arguments
that I give it. (1, 2, 3, 4, 5).
So sum of the tuple (1, 2, 3, 4, 5) is going to
give us the right answer.
It's going to print for us
the answer, 15.
Very, very cool.
So this way we can extend and use our *args to have
in a function that can accept any number of arguments.
So what is this one now?
Well, this one allows us to use keyword arguments.
For example, let's say I have the **kwargs like this.
And by the way, this can technically be anything.
So this is a variable that we're creating,
so I can name it hoooooo if I wanted to,
but the standard is to name it args and kwargs.
Because other developers are using it this way
and it's just the way it's done in the Python community.
Now, with the kwargs, as you might have guessed,
I can add keywords
like num1=5
and then num2=10.
So that if I print the kwargs and I click run
I get a dictionary of num1 equals to five
and num2 equals to 10.
So I can actually do something like sum plus sum
of items in the kwargs
and grab the values, right?
We've seen that before.
And let's actually grab this and do a for loop
and say items in kwargs.values, remember?
To grab the values over here, and in here just
add up the totals.
So let's say that in here
total equals or plus equals items.
And we'll add a total in here that is initially zero.
So that sum will be sum(args) plus total.
If I run this,
I get 30,
which looks about right.
Now, this does look a little confusing. So let's go over it.
We have the *args which allow us to grab these
positional arguments and just sum everything.
And we also have kwargs which allow us to grab any number of
keyword arguments and get a dictionary,
which comes as kwargs, and then use them however we want.
In our case, we're looping over all the values,
so items in kwargs.values.
And then I'm just going to total all those items,
have a total and just return the sum.
Now this is, again, extremely useful
because our super_func can now take as many arguments,
as many positional and keyword arguments as we want.
Now, one final thing.
There is a rule of the ordering that we can do
of our parameters here, right?
So the order, the rule is this.
First in our parameters we have, well, our actual params.
Then we can do *args,
then default parameters,
then **kwargs.
Let me show you what I mean.
If we wanna define this function, we wanna make sure
that if we give it a parameter of, let's say name,
that should come before *args.
And if we have default parameters, that should usually come
after args, but before kwargs.
So if I do, let's say i='hi'
and make sure we add a comma here.
We now have, following the rule, and by the way
you would never actually write a function like this
because, well, frankly, it looks super confusing.
Usually you're only using two of these
or maybe just one of these.
But this way I can call this function with name, Andy,
then my args, then my default parameter,
which will be i equals to 'hi',
which let's say we don't even pass in.
And then we have this.
So if we run here, this still works.
We are just not using name and i,
but you see that I'm following the rule of params,
args, default parameters, and then kwargs.
All right, hopefully your head doesn't hurt too much
after this one.
Take a break and I'll see you in the next one.
Bye-bye.