The meaning of * in front of python parameters

Excess parameters

know what running a function parameter is usually impossible. Another situation is that one function can manipulate many objects. What's more, the function that calls itself becomes a kind of api for usable applications.

For these situations, Python provides two special methods to define the parameters of the function, allowing the function to accept excessive parameters without explicitly declaring the parameters. These "extra" parameters will be explained in the next step.

Note that args and kwargs are just python conventions. You can name any function parameter in the way you like, but it is best to be consistent with the Python standard idiom so that your code can be easily read by other programmers.

Location parameters

using an asterisk before the parameter name, is to allow the function to accept an arbitrary number of positional parameters.

>>> def multiply(*args):
... total = 1
... for arg in args:
... total *= arg
... return total
...
>>> multiply(2, 3)
6
> >> multiply(2, 3, 4, 5, 6)
720

python collects the parameters into a tuple as the variable args. If there is no positional parameter besides the explicitly declared parameter, this parameter is treated as an empty tuple.

Keyword parameters

Python uses 2 asterisks before the parameter name to support any number of keyword parameters.

>>> def accept(**kwargs):
... for keyword, value in kwargs.items():
... print "%s => %r"% (keyword, value)
...
>>> accept (foo='bar', spam='eggs')
foo =>'bar'
spam =>'eggs'

Note: kwargs is a normal python dictionary type, including parameter names and values. If there are no more keyword arguments, kwargs is an empty dictionary.

Mixing parameter type

any positional parameters and keyword parameters can be used with other standard parameter declarations. Be careful when mixing, because their order in python is important. Parameters are classified into 4 categories, not all categories are required. They must be defined in the following order, and those not used can be skipped.

1) Required parameters
2) Optional parameters
3) Excessive positional parameters
4) Excessive keyword parameters

def complex_function(a, b=None, *c, **d):

This order is necessary because *args and **kwargs only accept any other parameters that are not included. Without this order, when you call a function with positional parameters, Python does not know which value is the desired value for the declared parameter, nor does it know which value is treated as an excess parameter.

It should also be noted that when the function can accept many required and optional parameters, it only needs to define an excessive number of parameter types.

Transmission parameter set

in addition to a function to accept an arbitrary set of parameters, Python code can also call a function with an arbitrary number of multiple, by an asterisk in front of said image. The parameters passed in this way are expanded into a parameter list by python. So that the function to be called
is not necessary to use an excess of such parameters call away. Any callable in python can be called with this technique. And use the same sequence rules and standard parameters together.

>>> def add(a, b, c):
... return a + b + c
...
>>> add(1, 2, 3)
6
>>> add(a=4, b=5, c=6)
15
>>> args = (2, 3)
>>> add(1, *args)
6
>>> kwargs=('b': 8,'c': 9)
>>> add(a =7, **kwargs)
24
>>> add(a=7, *args)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add() got multiple values ​​for keyword argument'a'


Pay attention to the last few lines of this example, pay special attention to when passing a yuan When the group is used as an excessive positional parameter, whether to explicitly pass the keyword parameter. Because python uses order rules to expand excess parameters, positional parameters should be placed first. In this example, the last two calls are the same, and python cannot determine which value is for a.

Guess you like

Origin blog.csdn.net/qq_37823979/article/details/109202996