Variable number of arguments
Lists and dictionaries may be used to define or call functions with a variable number of arguments. Let's define a list and a dictionary as follows:
data = [[1,2],[3,4]] style = dict({'linewidth':3,'marker':'o','color':'green'})
Then we can call the plot
function using starred (*
) arguments:
plot(*data,**style)
A variable name prefixed by *
, such as *data
in the preceding example, means that a list that gets unpacked in the function call is provided. In this way, a list generates positional arguments. Similarly, a variable name prefixed by **
, such as **style
in the example, unpacks a dictionary to keyword arguments. Refer to the following figure (Figure 7.1):
Figure 7.1: Starred arguments in function calls
You might ...
Get Scientific Computing with Python 3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.