First, parameter incoming rule
Variable parameters allow 0 or more parameters to be passed in and automatically assembled into a tuple when the function is called;
Keyword parameters allow 0 or more parameters to be passed in and automatically assembled into a dict when the function is called;
1. Incoming variable parameters:
Def calc (*numbers):
sum = 0 for
n in numbers:
sum = SUM + N * n * return
sum
The above definition functions, using the following:
Pass in multiple parameters,
Pass in a list,
Nums = [1, 2, 3]
calc (*nums) # Pass the element in the list as a variable parameter to the function
14 # function return value
2. Incoming keyword parameters:
>>> 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 function:
>>> info = {' City ': ' Guangzhou ', ' Job ': ' Engineer '}
>>>
>>> person (' LUHC ', 24, * * info)
NAME:LUHC age:24 Other: {' City ': ' Guangzhou ', ' Job ': ' Engineer '}
Note: A copy of the parameter info is obtained by the function person, and changes in the function do not affect the value of info
3. In the keyword parameter, you can restrict the name of the keyword parameter:
# by * Split to specify keyword parameter name
>>> def person (name, age, *, City, Job):
... Print (' name: ', Name, ' Age: ', Age, ' City: ', City, ' job: ', job ')
...
>>>
>>> person (' LUHC ', city= ' Guangzhou ', job= ' engineer ')
NAME:LUHC age:24 City: Guangzhou job:engineer
# If the parameter name is not in the defined range, the exception >>> person will be thrown
(' LUHC ', #, city= ' Guangzhou ', Jobs= ' engineer ')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Typeerror:person () got an unexpected keyword argument ' jobs '
>>>
In addition, if a variable parameter has been specified in the function, the * can be omitted as follows:
# omitted to be split with *, specify keyword 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
: (' A ', ' B ') City:guangz job:engineer
>>>
# Similarly, if you pass in a parameter name that is not specified by the keyword parameter, the exception >>> person is thrown
(' LUHC ' ,, ' A ', ' B ', city= ' Guangz ', job= ' engineer ', test= ' a ')
Traceback (most recent call last):
File <stdin> ", line 1, in <module>
Typeerror:person () got a unexpected keyword argument ' test '
>>>
Second, the combination of the use of parameters:
The order of the parameter definitions must be: required, default, variable, named keyword, and key 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)
Above is what this article is all about, and hopefully it will help you understand the passing of Python's function parameters