Working with *args and **kwargs in Python
Coding with Python
🕑 This lesson will take about 10 minutes
Parameters and arguments
When defining a new function, you can optionally specify one or more parameters that you want the function to use whenever it is called. These parameters are given names and are identified between the parentheses after the function’s name when it is created. Each parameter is separated by a comma. You can then use those parameters in the function's code. When the function is called, you specify the real values (called arguments) that will be passed into the function and represented by the parameter names in the function.
Check out the example code below. In this example, a function called calculate_area() has been created which will calculate the area of a rectangle. The function has two parameters - length and width. It then uses the print() function to output the length multiplied by the width. When the function is called, real values are provided for the length and width parameters. In this example, 10 has been provided for length and 5 has been provided for width. These values provided for the parameters are called arguments. In Python, "args" is a commonly used term that refers to the arguments passed to a function (the term "args" is short for "arguments".).
Working with *args in functions
In the example above (using a function with parameters), there are a fixed number of arguments (two arguments - length and width). For the function to work properly, two arguments must be provided to the function when it is called.
However, there are situations where you might want to create a function that can accept a variable number of arguments (that is, a function that can accept a different amount of arguments each time it is called). This is where you might use the *args syntax in Python.
Using the *args syntax allows a function to accept any number of positional arguments. This means you can call a function with different numbers of arguments. In the example code below, the function called myFunction is called twice using a different number of arguments.
Note that *args is just a naming convention. You can use any other name with the asterisk () character in your parameter list to achieve the same functionality. For example, you could write *values, *arguments, *items, etc.
Working with **kwargs in Python
In Python, **kwargs syntax (kwargs stands for keyword arguments) allows you to pass a variable number of keyword arguments to a function. When defining a function, you can use the **kwargs syntax when specifying the parameter list which allows you to collect the keyword arguments into a dictionary. Keyword arguments means that each argument has a key and a value.
In the example below, when the function is called, **kwargs collects some keyword arguments (first_name, last_name, city) into a dictionary named kwargs. The function then uses a for loop to iterate over the dictionary and print each key-value pair. You can also use a different number of keyword arguments each time you call the function.
You can also use **kwargs (keyword arguments) along with regular positional arguments in a function. In the example code below, two regular arguments are specified in the parameter list, followed by **kwargs. When the function is called, two values are provided as arguments (arg1 and arg2) along with keyword arguments.
Using *args and **kwargs can provide more flexibility in your functions, allowing them to accept a varying number of arguments or keyword arguments. These features of Python also allow you to pass key-value pairs into a function without explicitly defining all of the possible arguments or number of arguments in the function definition.
Next lesson: Date and time