multiple arguments, put in a tuple, start with a *, you can pass multiple parameters, * * is the parameter in the value of the keyword to pass the value of the extra values in a dictionary to render
*args: (indicates that the value is passed by location in the argument, and the extra value is given to args and rendered in ganso manner)
Example:
def foo (x,*args): print (x) print (args) foo (1,2,3,4,5) #其中的2, 3,4,5 has given args
The execution results are:
1 (2, 3, 4, 5)
When args is mixed with positional parameters and default parameters: (Note the order of the three)
Example one, (three order is: positional parameter, default parameter, *args)
def foo (X,y=1,*args): Print ( x) print (y) print (args) foo (1,2,3,4,5) #其中的x为1, the value of Y=1 is reset by 2, 3,4,5 gives args
The execution results are:
12 (3, 4, 5)
Example two, (three order is: position parameter, *args, default parameter)
def foo (x,*args,y=1): Print ( x) print (args) print (y) foo (1,2,3,4,5) #其中的x为1, 2,3,4,5 gives args, Y is still 1 according to the default parameter
The execution results are:
1 (2, 3, 4, 5) 1
About *, can be viewed from 2 points of view (need to split):
1, from the perspective of formal parameters:
Example:
def foo (*args): #其实这一操作相当于def foo (a,b,c,d,e): print (args) foo (1,2,3,4,5) #其中的1, 2,3,4,5 are passed to the a,b,c,d,e according to the location values respectively
The execution results are:
(1, 2, 3, 4, 5)
2, from the point of view of the actual argument:
Example:
def foo (z): print ( x) print (y) print (z) foo (* ()) #其中的 * (All-in-one) take it apart: foo, are transmitted to X, Y, z, respectively, by location.
The execution results are:
123
——————————————————————————————————————————————————————————————————————————————————————
**kwargs: (indicates that the value of the parameter is presented in a dictionary by the values of the key)
Example:
def foo (x,**kwargs): print (x) print (Kwargs) foo (1,y=1,a=2,b=3,c=4) #将y =1,a=2,b=3,c=4 gave Kwargs in a dictionary
The execution results are:
1{' y ': 1, ' a ': 2, ' B ': 3, ' C ': 4}
Questions about **kwargs with positional parameters, *args, default parameters: (note order)
Position parameters, *args, **kwargs the order must be positional parameters, *args, **kwargs, or will be error:
Example:
def foo (x,*args,**kwargs): print (x) print (args) print (Kwargs) foo (1,2,3,4,y=1,a=2,b=3,c=4) #将1传给了x, 2 , 3,4 was passed to the args,y=1,a=2,b=3,c=4 in the form of a dictionary to Kwargs
The execution results are:
1 (2, 3, 4) {' Y ': 1, ' a ': 2, ' B ': 3, ' C ': 4}
Error Example: (due to sequential error)
def foo (x,**kwargs,*args): print (x) print (args) print (Kwargs) foo (1,y=1,a=2,b=3,c=4,2,3,4)
The result of the execution will be an error:
Syntaxerror:invalid syntax
Position parameters, default parameters, **kwargs the order must be positional parameters, default parameters, **kwargs, or will be error:
Example:
def foo (X,y=1,**kwargs): Print ( x) print (y) print (Kwargs) foo (1,a=2,b=3,c=4) #将1按照位置传值给x, y by default parameter is 1,a =2,b=3,c=4 gave Kwargs in a dictionary way.
The execution results are:
11{' A ': 2, ' B ': 3, ' C ': 4}
About * *, can be viewed from 2 points of view (need to split):
1, from the perspective of formal parameters:
Example:
def foo (**kwargs): #其实就是相当于def foo (y,a,b,c) print (Kwargs) foo (y=1,a=2,b=3,c=4)
The execution results are:
{' Y ': 1, ' a ': 2, ' B ': 3, ' C ': 4}
2, from the point of view of the actual argument:
Example one:
def foo (a,b,c,d): print ( a) print (b) print (c) print (d) Foo (**{"a": 2, "B": 3, "C": 4, "D": 5}) #**{"A": 2, "B": 3, "C": 4, "D": 5} is passed to a,b,c,d for each value in the dictionary as a keyword
The execution results are:
2345
Example two:
def foo (a,b,c,d=1): print ( a) print (b) print (c) print (d) Foo (**{"a": 2, "B": 3, "C": 4}) #**{"A": 2, " B ": 3," C ": 4} is to pass each value in the dictionary according to the value of the keyword passed to a,b,c;d still follow the default parameters
The execution results are:
2341
Python-based functions-in formal parameters: *args and **kwargs