In the definition, a function adds a star number before a parameter to convert multiple parameters passed in into an object, tuples, or dictionary, it can be said that these parameter values are collected and the function is defined. by adding a star number before the parameter, the passed parameters are converted into an object, tuples, or dictionary, it can be said that these parameter values are collected.
Adding an asterisk before the parameter indicates that all values are placed in the same tuples. The return value of this parameter is a tuple.
Adding two asterisks before the parameter indicates that all values are placed in the same dictionary, and the return value of this parameter is a dictionary.
>>> Def print_param (x, y, z = 3, * pospar, ** keypar): print x, y, z print pospar print keypar >>> print_param (3, 4, 5, 6, 7, 8, m = 1, n = 2) 3 4 5 (6, 7, 8) {'M': 1, 'n': 2}
Allocation parameters
When a function is called, by adding a star number before the parameter, the transmitted parameter must be a tuples or dictionary, and the value can be converted to the value of the corresponding variable, this process can be seen as the allocation of parameter values.
Add an asterisk before the parameter to indicate that the value of the tuples is allocated to the corresponding function parameter value.
The first two asterisks of the parameter indicate that the value in the dictionary is assigned to the key, and the key must be used as the parameter name in the function definition.
>>> Def add (x, y): print x, y return x + y >>>> add (* param) 1 23 >>> def test (name, age ): print name, age >>> m = {'name': 'Xiaoli', 'age': '12' }>>> test (** m) xiaoli 12 >>> m = {'name': 'Xiaoli', 'age': 12 }>>> test (** m) xiaoli 12