Arbitrary arguments
1. Arbitrary arguments
Let's finish up by discussing two techniques for adding flexibility to custom functions: 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 one argument called values? The way we have designed this function means that the intended data structure is a list or set, so if we tried to provide another structure such as a dictionary it wouldn't work. This means we need to think about the data structure we need as an input to the function and the number of arguments the function should accept. So far, we only accept one argument for the average function that we created, which we presume to be a list or set. But what would happen if a developer tries to pass more than one argument presuming that the function will still calculate their average? Assume, for example, that they enter six individual integer values separated by commas. 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 in using our custom function. With a slight tweak to our function, we can make it accept what are called arbitrary arguments, resulting in a function that 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-point-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 is the same as, again, 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 that we've seen 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 in the form of 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 split into multiple kwargs and use pairs of asterisks in front of each argument, which again combines 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.