- Python Shortcuts, Commands, and Packages
- 4.2 Twenty-Two Programming Shortcuts
- 4.3 Running Python from the Command Line
- 4.4 Writing and Using Doc Strings
- 4.5 Importing Packages
- 4.6 A Guided Tour of Python Packages
- 4.7 Functions as First-Class Objects
- 4.8 Variable-Length Argument Lists
- 4.9 Decorators and Function Profilers
- 4.10 Generators
- 4.11 Accessing Command-Line Arguments
- Chapter 4 Summary
- Chapter 4 Questions for Review
- Chapter 4 Suggested Problems
4.8 Variable-Length Argument Lists
One of the most versatile features of Python is the ability to access variable-length argument lists. With this capability, your functions can, if you choose, handle any number of arguments—much as the built-in print function does.
The variable-length argument ability extends to the use of named arguments, also called “keyword arguments.”
4.8.1 The *args List
The *args syntax can be used to access argument lists of any length.
def func_name([ordinary_args,] *args): statements
The brackets are used in this case to show that *args may optionally be preceded by any number of ordinary positional arguments, represented here as ordinary_args. The use of such arguments is always optional.
In this syntax, the name args can actually be any symbolic name you want. By convention, Python programs use the name args for this purpose.
The symbolic name args is then interpreted as a Python list like any other; you expand it by indexing it or using it in a for loop. You can also take its length as needed. Here’s an example:
def my_var_func(*args): print('The number of args is', len(args)) for item in args: print(items)
This function, my_var_func, can be used with argument lists of any length.
>>> my_var_func(10, 20, 30, 40) The number of args is 4 10 20 30 40
A more useful function would be one that took any number of numeric arguments and returned the average. Here’s an easy way to write that function.
def avg(*args): return sum(args)/len(args)
Now we can call the function with a different number of arguments each time.
>>> avg(11, 22, 33) 22.0 >>> avg(1, 2) 1.5
The advantage of writing the function this way is that no brackets are needed when you call this function. The arguments are interpreted as if they were elements of a list, but you pass these arguments without list syntax.
What about the ordinary arguments we mentioned earlier? Additional arguments, not included in the list *args, must either precede *args in the argument list or be keyword arguments.
For example, let’s revisit the avg example. Suppose we want a separate argument that specifies what units we’re using. Because units is not a keyword argument, it must appear at the beginning of the list, in front of *args.
def avg(units, *args): print (sum(args)/len(args), units)
Here’s a sample use:
>>> avg('inches', 11, 22, 33) 22.0 inches
This function is valid because the ordinary argument, units, precedes the argument list, *args.
4.8.2 The “**kwargs” List
The more complete syntax supports keyword arguments, which are named arguments during a function call. For example, in the following call to the print function, the end and sep arguments are named.
print(10, 20, 30, end='.', sep=',')
The more complete function syntax recognizes both unnamed and named arguments.
def func_name([ordinary_args,] * args, **kwargs): statements
As with the symbolic name args, the symbolic name kwargs can actually be any name, but by convention, Python programmers use kwargs.
Within the function definition, kwargs refers to a dictionary in which each key-value pair is a string containing a named argument (as the key) and a value, which is the argument value passed.
An example should clarify. Assume you define a function as follows:
def pr_named_vals(**kwargs): for k in kwargs: print(k, ':', kwargs[k])
This function cycles through the dictionary represented by kwargs, printing both the key values (corresponding to argument names) and the corresponding values, which have been passed to the arguments.
>>> pr_named_vals(a=10, b=20, c=30) a : 10 b : 20 c : 30
A function definition may combine any number of named arguments, referred to by kwargs, with any number of arguments that are not named, referred to by args. Here is a function definition that does exactly that.
The following example defines such a function and then calls it.
def pr_vals_2(*args, **kwargs): for i in args: print(i) for k in kwargs: print(k, ':', kwargs[k]) pr_vals_2(1, 2, 3, -4, a=100, b=200)
This miniprogram, when run as a script, prints the following:
1 2 3 -4 a : 100 b : 200