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 this 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 funk.
For super function that receives some sort of arguments.
And these arguments that we're going to receive.
And remember, although I'm calling these arcs.
This itself is a parameter, remember?
But let's say that we want to just return some of the arguments and you might be wondering why some
don't we have to define that function?
Well, some actually exists in Python, as you can see over here.
So we can just return the some of the arguments.
So let's try this.
Let's say super func.
And give it arguments.
One, two, three, four, five.
If I click run.
Hmm.
I get in air super funk takes one positional argument but five where given and that makes sense right?
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 star arcs and we run this function.
We see that the print star arcs 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.
One, two, three, four, five.
So some of the tuple one, two, three, four, five 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 star args to have 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 star star keyword arcs 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 hoo if I wanted to, but the standard is to name it args and keyword args because other
developers are using it this way and it's just the way it's done in the Python community.
Now with the keyword arcs, as you might have guessed, I can add keywords like num one equals.
Five and then num two equals ten.
So that if I print the key word args and I click run.
I get a dictionary of none.
One equals to five and now two equals to ten.
So I can actually do something like sum plus sum of items in the keyword args.
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 keyword arcs dot 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 some.
Will be some 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 star arcs which allow us to grab these positional arguments and just sum everything.
And we also have keyword arcs which allow us to grab any number of keyword arguments and get a dictionary
which comes as keyword arcs and then use them however we want.
In our case, we're looping over all the values, so items in keyword arc 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 Superfund can now take as many arguments, as many positional
and keyword arguments as we want.
Now, one final thing.
There is a role 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 star arcs, then default parameters.
Then Star.
Star.
Keyword arcs.
Let me show you what I mean.
If we want to define this function, we want to make sure that if we give it a parameter of, let's
say name.
That should come before star arcs.
And if we have default parameters, that should usually come after arcs, but before keyword arcs.
So if I do, let's say I equals high 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 and then my args, then my default parameter, which
will be I equals to high, 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 keyword arx.
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.