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
Keyboard Parts.
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 this 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 of what some actually exists in Python, as you can see over here.
So we can just return to some of the arguments.
So let's try this.
Let's say super func.
And give it arguments, one, two, three, four or five, if I click, run.
Hmm, I get in their super funk, takes one positional argument, but five where they 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 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 starts 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, Tuffin.
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 keyboard 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 who if I wanted to.
But the standard is to name it ARGs and keyword arcs because other developers are using it this way
and it's just the way it's done in the Python community.
Now, with the key word, as you might have guessed, I can add keywords like number one equals.
Five and then two equals 10.
So that if I print the key word ARGs and I click run.
I get a dictionary of numb one equals to five, and now I'm two equals to 10, so I can actually do
something like some plus some of the items in the key word 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.
And here, that is initially zero, so that some.
Will be some ARC's plus total.
If I run this.
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 key word arcs which allow us to grab any number of keyword arguments and get a dictionary,
which comes as key word arcs and then use them however we want.
In our case, we're looping over all the values, so items and keyword are values and then I'm just
going to total all those items.
Have a total and just returned the some.
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 all 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 dysfunction, we want to make sure that if we give
it a parameter of, let's say name.
That should come before Stathakis, and if we have default parameters, that should usually come after
arcs but before key word 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 Andy.
Then my arms, 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 parents,
ARC's default parameters and then keyword ARC's.
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.