Home > Article > Backend Development > What are the uses of ** and * parameters in Python?
In Python, the "**" parameter indicates that any number of keyword parameters are accepted here. These parameters are saved in the form of a dictionary, which will be interpreted as a dictionary; the "*" parameter indicates that any number of keyword parameters are accepted here. Multiple non-keyword parameters, these parameters will be saved in the form of an array, that is, they will be interpreted as a tuple.
Python's parameter passing mechanism has value passing (value data types such as int, float, etc.) and reference passing (non-value object data types such as dictionaries and lists). Represents) two basic mechanisms and the convenient keyword transfer feature (directly use the formal parameter name of the function to specify the transfer target of the actual parameters. For example, the function is defined as def f(a,b,c), then f( can be used when calling b=1,c=2,a=3) specifies the transfer method of the formal parameter target, without having to stick to the positional correspondence between formal parameters and actual parameters such as C language) In addition, python also allows wrapping methods Parameter passing, which provides the basis for function calls with uncertain number and type of parameters.
For example: def f(*a,**b), the implementation of wrapped parameter transfer is to add * or ** in front of the formal parameter when defining the function, and the formal parameter corresponding to * (such as the above a) will be interpreted as a tuple, and the formal parameter corresponding to ** (such as b above) will be interpreted as a dictionary. For the specific parameter transfer during the call, see the following code:
The result of running the above code is:
(3, 4)
{'n': 2, 'm': 1}
It can be seen that for different Variables passed using keywords will be passed to *a as part of the tuple, while variables passed using keywords will be passed to **b as part of the dictionary.
At the same time, there is a tricky place, Python stipulates that variables passed by non-keywords must be written in front of variables passed by keywords, so when using * and ** together, the * formal parameter must be in front of the ** formal parameter.
In addition, when doing When the function is called, there is a method called unwrapping:
The output of the above code is consistent with the previous one.
Put the element When a group or dictionary is passed in as a parameter, if you want to adapt the formal parameter definition in the wrapped form (such as passing h to *a and k to **b above), use * for tuples and ** for dictionaries." Just unwrap" and pass it.
In fact, using * when calling f is to remind Python: I want to split the actual parameter h into two scattered elements c and d, and pass them separately ( It is also possible to define f in the above code as def f (arg1, arg2, **b), so that arg1 will get the value 3 and arg2 will get the value 4). ** The same principle is similar. In addition, when unpacking *This also applies to lists ([] defines lists, () defines tuples).
The above is the detailed content of What are the uses of ** and * parameters in Python?. For more information, please follow other related articles on the PHP Chinese website!