Transcripted Summary

In this lesson, we're going to work with args and kwargs, or arguments and keyword arguments in Python.

# Positional Arguments

We'll just make a simple function to explore how argument and keyword arguments work.

But it's important to keep in mind that this is actually really frequently used throughout Selenium coding, so it's good to try and understand how these things work.

So, going ahead, doing my function definition and giving the arguments.



Now you'll notice that all of the names of my variables are in gray. That's because they're not being used, and so the IDE likes to call that out.

So, they will be used.

Since the whole point of this function is just to print this output and to understand how these arguments work, I'm actually just going to make myself a docstring (using 3 single quotes in a row at the start and end), which is very similar to a multi-string comment that tells me what this function does.

Okay.

So now I'm going to go ahead and do my pretty-printing.

Okay, and we have 2 parentheses here. The inner parentheses match the format and then the outer parentheses match our print statement, so that's good.

Now I'll go ahead, and I will call my function (and you'll notice it pops right up, which is great) and so, I'm going to provide the arguments now.

It's important to know that you may have errors and that's okay, you can go back, correct, double check.


def user_info(name, age, city):

    '''This function prints name, age, and city from an argument provided to the function.'''

    print("{} is {} years old, from {}".format(name, age, city))

user_info("Janet", 58, "Oklahama City")

We'll go ahead and run this.

Here we go.



So, we ran the function and it took all of our variables from the argument and input them.

But what happens if we do this function again, and we don't give a name?

Let's say we just go with the age and the city.



Okay, so now we have an error because we are missing the number of arguments that the function expects.

And actually, I have an error because I didn't put my String data in a String, so it didn't really know what to do with that.

So, let's try this again, there we go.

Now I'm getting the correct error, which is great.



And so, it's telling me I am missing 1 required positional argument and that's city, so Python doesn't check the input to see that it matches what we might expect.

Python just has a name, an age, and a city, 3 variables, and those variables are waiting to get information assigned to them, so that way that the function can run in the way that it's intended to run.

So here Python is reading this is the name, this is the age and it's telling me that I'm missing the city.

These are positional arguments because Python is reading these arguments_ in order_, in the position that they are given in the function definition.


# Keyword Arguments

We can use something called keyword arguments in order to overcome some of the limitations of positional arguments, if we have many arguments that we would like to provide to a function, or many options that we could provide, but we don't necessarily need to provide everything all the time.

Let's take a look at how keyword arguments differ from positional arguments.

First of all, we can set some defaults when we're using keyword arguments, so that way that our function knows what to do if we don't provide the value that it's looking for, for any of the arguments.

  • So, in this fictional example, everybody who does not have a city entered in the function will now be from Tucson.

  • The default age for anybody who doesn't have an age will be zero years old.

  • And we'll go ahead and leave the name as an argument that will require definition within our function.

So, since we have default for both age and city, we can go ahead and test to ensure that this is working as expected by providing a name argument and leaving the other 2 arguments blank.


#args & kwargs
def user_info(name, age=0, city="Tucson"):

    '''This functoin prints name, age, and city from an argument provided to the function.'''

    print("{} is {} years old, from {}".format(name, age, city))

user_info("Janet", 58, "Oklahama City")

user_info("Micah")

Then when we run our code, we can make sure that our outcome shows that the arguments used are the default arguments that we provided in the function.

And indeed, this is the case, so that's great.



So now you can see we have a little helpful pop up that gives us the name and the age and tells us what we need to do.

And with keyword arguments, you don't need to follow the positional order of the argument.

You can adjust that and provide it as you want.


user_info(age=56, name="Kadeem")

As long as you have a default specified for anything you're not providing, you will be able to overwrite the values of the defaults that you do provide and any arguments that are required, of course you'll need to provide as well.



So, we can go ahead and take a look.

  • We've provided a name

  • We've said that Kadeen is 56 years old, so we've overwritten the default of zero

  • And we've continued to use the default location


# *args & **kwargs

Now that we know what positional and keyword arguments do, let's learn about the special *args and **kwargs syntax and Python.

The *args allows a function to take any number of positional arguments.



The **kwargs allows a function to take any number of keyword arguments.



Any formal or required arguments that you wish to pass into a function, must be passed before *args and **kwargs.



In this example, you can see that we have the fname, lname, email, and company formal positional arguments followed by the *args and **kwargs.

When we use the function, we pass the arguments into the function first, starting with the required arguments.

Then we pass in a *arg as a salary for the employee and a **kwarg as the hire date.


# *args and **kwargs
def application(fname, lname, email, company, *args, **kwargs):
    print("{} {} works at {}. Her email is {}.".format(fname, lname, company, email))

application("Jess", "Ingrass", "mail @ mail.com", "Teach Code", 75000, hire_date = "September 2010")

I encourage you to pause the video here and experiment with different variations of this code, changing the formal arguments as well as experimenting with different numbers of *args and **kwargs.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR