I. Parameter pass-through rule
Variable parameters allow 0 or any parameter to be passed in, automatically assembled into a tuple when the function is called;
The keyword parameter allows 0 or any parameter to be passed in, which is automatically assembled into a dict when the function is called;
1. Pass in the variable parameter:
Def calc (*numbers): sum = 0 for n in numbers: sum = SUM + N * n return sum
The above definition functions are used as follows:
Pass in multiple parameters,
Calc (1, 2, 3, 4) #函数返回值
Pass in a list,
Nums = [1, 2, 3]calc (*nums) # passed the element in list as a variable parameter to the function 14 # function return value
2. Incoming keyword parameter:
>>> def person (name, age, **kw): ... Print (' name: ', Name, ' Age: ', age, ' other: ', kw) ... >>> >>> person (' LUHC ', city= ' Guangzhou ') name: LUHC age:24 Other: {' City ': ' Guangzhou '}
Similarly, you can pass a predefined dict as a parameter to the above functions:
>>> info = {' City ': ' Guangzhou ', ' job ': ' Engineer '}>>> >>> person (' luhc ', ' **info ') Name:luh C age:24 Other: {' City ': ' Guangzhou ', ' job ': ' Engineer '}
Note: The function person obtains a copy of the parameter info, and the change within the function does not affect the value of info
3. In the keyword parameter, you can restrict the name of the keyword parameter:
# by * Split to specify key parameter name >>> def person (name, age, *, City, Job): ... Print (' name: ', Name, ' Age: ', Age, ' City: ', City, ' job: ', job) ... >>> >>> person (' LUHC ', city= ' Gu Angzhou ', job= ' engineer ') NAME:LUHC age:24 City:guangzhou job:engineer# If the parameter name is not within the defined range in the passed in argument, an exception will be thrown >>> Person (' LUHC ', +, city= ' Guangzhou ', jobs= ' engineer ') Traceback (most recent call last): File "
", line 1, in< c4/>
Typeerror:person () got an unexpected keyword argument ' jobs ' >>>
In addition, if a variable parameter is already specified in the function, the * can be omitted, as follows:
# omitted with * as partition, specify key parameter name >>> def person (name, age, *args, City, Job): ... Print (' name: ', Name, ' Age: ', age, ' args: ', args, ' City: ', City, ' job: ', job) ... >>> >>> person (' LUHC ', +, ' a ', ' B ', city= ' Guangz ', job= ' engineer ') NAME:LUHC age:24 args: (' A ', ' B ') City:guangz Job:engineer>> ;> # Similarly, if you pass in a parameter name that is not specified by the keyword argument, throw an exception >>> person (' LUHC ', ' a ', ' a ', ' B ', city= ' Guangz ', job= ' engineer ', test= ' a ') Traceback (most recent): File "
", line 1, in
Typeerror:person () got an unexpected ke Yword argument ' test ' >>>
Two, the parameter combination uses:
The order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters
Def f1 (A, B, c=0, *args, **kw): print (' A = ', A, ' B = ', B, ' C = ', C, ' args = ', args, ' kw = ', kw) def F2 (A, B, c=0, *, D , **kw): print (' A = ', A, ' B = ', B, ' C = ', C, ' d = ', D, ' kw = ', kw)
The above is the whole content of this article to you, hoping to understand the Python function parameters of the transfer of some help