|
Collection parameters:
In the function definition, the collection of location parameters do not match the tuple.
>>> Def f (* args): print (args)
...
When this function is called, python relevant parameters for all positions collected into a new Ganso, and the assignment of this tuple to the variable args. (Can be indexed or stepping in for loop)
>>> F ()
()
>>> F (1)
(1,)
>>> F (1, 2, 3, 4)
(1, 2, 3, 4)
** Similar characteristics, but it is only valid for keyword arguments. These keyword arguments passed to the new dictionary.
>>> Def f (** args): print ((args)
...
>>> F ()
{}
>>> F (a = 1, b = 2)
{ 'A': 1, 'b': 2}
Another example:
>>> Def f (a, * pargs, ** kargs): print (a, pargs, kargs)
...
>>> F (1, 2, 3, x = 1, y = 2)
1 (2, 3) { 'y': 2, 'x': 1}
Unpack parameters
When you call the * syntax unpack parameter set.
>>> Def func (a, b, c, d): print (a, b, c, d)
...
>>> Args = (1, 2)
>>> Args + = (3, 4)
>>> Func (* args)
1234
Similarly, ** will be key-value pairs unpack a dictionary.
>>> Args = { 'a': 1, 'b': 2, 'c': 3}
>>> Args [ 'd'] = 4
>>> Func (** args)
1234
to sum up
* / ** Syntax: in the head, it means that any number of parameters were collected, and the call is unpack it any number of arguments. |
|
|
|