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 key word 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 a super function
that receives some sort of arguments and these arguments that we're going to receive.
And remember although I'm calling these args 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 where.
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 sum 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
I get an error.
Super func takes 1 positional argument but 5 where.
Give it 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 but 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 ARGs and we run this function we see that the print star args gives us 1 2 3 4 5.
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 couple.
So args inside of this function is a topple of arguments that I give it.
One two three four five.
So some of the topple.
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 enough 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 key word args 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 who 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 1 equals 5 and then num two equals 10 so that if I print the key word ARGs
and I click Run I get a dictionary of num one equals 2 Five and num two equals to 10.
So I can actually do something like some 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 will add a total in here.
That is initially zero.
So that sum 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 args which allow us to grab these positional arguments and just some 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 super fund 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 programs then we can do star arcs then
default parameters then star star keyword parks.
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 you look 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 high which lets 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 bet you see that I'm following the rule of Paramus args default parameters and then keyword arcs.
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.
Bobby.