*,** as function arguments

From the page of matplotlib,


matplotlib.pyplot.hlines(y, xmin, xmax, colors=None, linestyles='solid', label='', *, data=None, **kwargs)

what does the ‘*’ and ‘**’ mean here? Variable arguments?

The *, indicates that this is the end of any positional arguments; all
arguments after this must be supplied by name i.e. the data argument
must be supplied by name.

The **kwargs gathers any other supplied names arguments into a
dictionary named kwargs. This can be handy when you’re writing a
function which is mostly a wrapper for another function: you can collect
thearguments you care about, and pass through anything else to the
second function.

In the above definition the parameters y, xmin, xmax, colors,
linestyles and label can all be supplied by position. For example:

 hlines(9, 0, 99, some_colours, 'dashed', 'label here')

would assign these values according to their position i.e. xmax would
be 99 in the above.

Three of the positional values have defaults: colors, linestyles and
label. You can leave any of these out. If you wanted to specify just
the label, you might write:

 hlines(9, 0, 99, label='label here')

This lets you supply that argument, but still skip colors and
linestyles. So you can supply the label either by naming it or
by supplying enough positional parameters to reach the label

By contrast, data must be supplied by name because it comes after
the *, in the parameter list.

You can also supply other named parameters, and they will land in

So this:

 hlines(9, 0, 99, label='label here', data=[1,2,3], extra=[5,6,7], something="foo")

would set:

 label='label here'
 kwargs={'extra':[5,6,7], 'something':'foo'}

Cameron Simpson cs@cskk.id.au


They also mean an arbitrary number and type of arguments, i.e., not fixed.
They’re generally called args and kwargs by convention, but they can be called anything you want to make them make sense to you in your program (i.e., *donuts, **cars, etc.).

For example, for the single asterisk argument (*) argument type, it can be used as (not all exhaustive):

some_list = [1,2,3,4]

def print_inputs(*args):  print(args)

print_inputs('today', 'tomorrow', 'whenever', 'wherever')

The double asterisk (**) type of argument, relates to an arbitrary number of keyword:value entered arguments (i.e., dictionary form type of inputs).

grade_info = {'A': 4.0, 'B': 3.0, 'C': 2.0}

def print_grades(**grades):
    for key in grades:


Hope this helps.

1 Like

I agree with your explanation of *args and *kwargs but the * in the original question is not equivalent to *args. Cameron’s explanation holds for that one.

1 Like

To lookup symbol meanings, go to 3.12.1 Documentation, click index in upper right, then symbols. to arrive here.

1 Like