- 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函数作为一类对象
- 4.8 Variable-Length Argument Lists
- 4.9 Decorators and Function Profilers
- 4.10 Generators
- 4.11 Accessing Command-Line Arguments
- 第四章总结
- 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-inprintfunction does.
The variable-length argument ability extends to the use of named arguments, also called “keyword arguments.”
4.8.1 The *args List
The*argssyntax can be used to access argument lists of any length.
deffunc_name([ordinary_args,]*args):statements
The brackets are used in this case to show that*argsmay optionally be preceded by any number of ordinary positional arguments, represented here asordinary_args. The use of such arguments is always optional.
In this syntax, the nameargscan actually be any symbolic name you want. By convention, Python programs use the nameargsfor this purpose.
The symbolic nameargsis then interpreted as a Python list like any other; you expand it by indexing it or using it in aforloop. You can also take its length as needed. Here’s an example:
defmy_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.
defavg(*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*argsin the argument list or be keyword arguments.
For example, let’s revisit theavgexample. Suppose we want a separate argument that specifies what units we’re using. Becauseunitsis not a keyword argument, it must appear at the beginning of the list, in front of*args.
defavg(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 theprintfunction, theendandseparguments are named.
print(10, 20, 30, end='.', sep=',')
The more complete function syntax recognizes both unnamed and named arguments.
deffunc_name([ordinary_args,]*args,**kwargs):statements
As with the symbolic nameargs, the symbolic namekwargscan actually be any name, but by convention, Python programmers usekwargs.
Within the function definition,kwargsrefers 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:
defpr_named_vals(**kwargs): for k in kwargs: print(k, ':', kwargs[k])
This function cycles through the dictionary represented bykwargs, printing both the key values (corresponding to argument names) and the corresponding values, which have been passed to the arguments.
For example:
>>>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 bykwargs, with any number of arguments that are not named, referred to byargs. Here is a function definition that does exactly that.
The following example defines such a function and then calls it.
defpr_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