Variable length parameter: variable length refers to the number of arguments is not fixed
Variable-length arguments defined by location: *
Variable-length arguments defined by keyword: * *
One, by location
def func (x,y,*args): Print(x, y)print(args) func ( 1,2,3,4,5) # * Process the extra arguments defined by location # and then assign the variable to the * to save it in the form of a tuple # args= (3,4,5)
View Code
1. Equivalent
def func (x,y,*args): Print(x, y)print(args) func ( # As with func (1,2,3,4,5), why the next principle
View Code
2. Principle
def func (x, y , z): Print (x, Y, Z) func (* (+) ) # *== position parameter will be opened ( # and then the assignment .
View Code
Second, by keyword
def func (x,y,**Kwargs): Print(x, y)print(Kwargs) func ( 1,y=2,z=3,a=1,b=3) # * * processing the extra argument defined by the keyword # and assigning it to * * After the variable to be saved into a dictionary form # kwargs={' z ': 3, ' a ': 1, ' B ': 3}
View Code
1. Equivalent
def func (x,y,**Kwargs): Print(x, y)print(Kwargs) func ( 1,y=2,**{'a': 1,'b': 3,'Z ': 3})# as with func (1,y=2,a=1,b=3,z=3), the principle in the next
View Code
2. Principle
deffunc (x, Y, z):Print(x, Y, Z) func (**{'y': 2,'x': 1,'Z': 3})The **== keyword parameter will {'y': 2,'x': 1,'Z': 3} disassemble#and then the assignment .View Code
python--function 8, variable length parameter