Arbitrary arguments
1. Arbitrary arguments
Let's wrap up working with custom functions by discussing two techniques to add more flexibility: arbitrary positional arguments and arbitrary keyword arguments.2. Limitations of defined arguments
Let's start by re-examining our custom average function. Notice that it expects a single argument called `values`. The way we've designed this function indicates that the intended data structure is a list or set, so if we try to provide a dictionary, it won't work. We need to consider the required data structure for input and the number of arguments the function accepts. Currently, it only accepts one argument, but what happens if a developer tries to pass six individual integer values separated by commas, assuming the function will calculate their average? We'll get an error because we've provided too many arguments!3. Arbitrary positional arguments
While docstrings help clarify how to use a function, Python provides other ways to support developers. With a slight tweak, we can make our function accept arbitrary arguments, meaning it works with any number of positional, non-keyword arguments! We can now provide one, five, or even one thousand arguments! To do this, we place an asterisk in front of a single argument. The convention is to write asterisk-args, but any argument name will work. This flexibility enables functions to be used in a variety of ways while still producing the expected results!4. Using arbitrary positional arguments
Now, calling the function with six comma-separated integer values produces the correct output of 13.33.5. Args create a single iterable
To Python, the asterisk means combining all positional arguments, so it places them all inside a tuple. If we have lots of data across different structures, such as lists, and want to find the average without needing to combine them into a single variable, we can place an asterisk in front of each argument. The output remains the same; all arguments are combined into a single structure.6. Arbitrary keyword arguments
We can also use arbitrary keyword arguments by adding two asterisks instead of one. The term generally used is kwargs, but any word will work. Remember the general syntax of keyword argument equals value. This is equivalent to the key-value pairs in dictionaries. To enable this, we first modify our function to accept arbitrary keyword arguments, then we need to change the code in the body of the function to presume dictionaries will be used, now working with the dot-values method of our kwargs.7. Using arbitrary keyword arguments
As with args, we can provide multiple comma-separated values. Except with kwargs, we must give each argument a name and assign a value, like so. The function combines the keyword argument names and values into a single dictionary and calculates the average of the values. We can also call average with a single keyword argument as a dictionary by using two asterisks in front. With the first approach, we manually assigned keyword argument names and values. In the second approach, using double asterisks, Python takes each dictionary key as a keyword argument and each value as the assigned value.8. Kwargs create a single iterable
As with args, we can use pairs of asterisks in front of multiple kwargs, which combines them into a single iterable and provides the same result!9. Let's practice!
Now it's your turn to add arbitrary arguments to custom functions.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.