Preface in Python, there are several ways to define and transfer function parameters:
syntax |
meaning |
def func (name) |
general parameters can To match the position according to the location, or you can match the |
def func (name=value) |
default parameter according to the key, and use the default value |
when the parameter is not passed.
def func (*iteratable) |
collects all remaining unmatched parameters into a tuple |
def func (**dictionary) |
collects the remaining unmatched parameters in a dict |
def func (*, name) |
must use key to match parameters |
def func (*other, name) |
must use key to match parameters |
func (value) |
function invocation, where parameter values are matched by the order of parameters that are passed |
func (name=value) |
function call, parameter values match key to |
func (*iteratable) |
function call to expand the parameters in the Iteratable container, matching the corresponding function arguments by position |
func (**dictionary) |
function Call, expand the arguments in Dict, and match the corresponding function arguments by key value |
In Python, parameters can be passed sequentially, and when a function is called, the values of the parameters are matched sequentially, from left to right, in the order in which they are passed. You can also pass a default value to a parameter, which is well understood, because in many languages, such as C, C + +, Java, the parameter passing of a function is passed in this way. But there are other features of Python's parameter definition and delivery, in addition to sequential delivery and default values, which are described in the order in which the parameters in the function call in Python are matched: In order, to assign a value to a parameter without a key, meaning that when the parameter is passed, Parameters that need to be matched sequentially must appear before the parameters that match the key; assigning values according to key matching, and returning redundant parameter values in order matching but not matching into *name's tuple, and assigning the parameter values matching the key on superfluous mismatches into the Dict object of **name ; The default value will be assigned to the parameter with the default value on the match
Second, according to the key matching parameters for C, C + + This language, in the call function, the system will first press the function address into the stack, followed by the parameters from the right to the left of the order, one indentation into the stack. Therefore, C, C + + languages They only support sequential matching of formal parameters. In contrast to Python, parameters can be matched in order, or by parameter names. such as: def func (name, age): Print (name, time) for this function, the following invocation is equivalent to: func (' Rechar ',) #按顺序匹配 func (name = ' Rechar '), and 27 #按参数名称匹配, the value of the system parameter name is ' Rechar ' at run time, and the value of age is func (age = +, name = ' Rechar ') #按参数名称匹配 func (' Rechar ', age = 27) # Name is matched sequentially, age is matched in Python by name, and when the parameter is matched by the parameter name, the order in which the parameters are passed is arbitrary and does not require passing in the order of the parameters in the function definition. When using name matching, if you need to mix and match the rules sequentially, the parameters that match in order must appear before the key-matching arguments: func (name = ' Rechar ', 27) The above call will report the following error:
Third, "*name" python in the function definition what happens if the caller's incoming arguments are found to be mismatched after assigning a value to the parameters that match sequentially and by key. Take a look at the following example: Func (' Rechar ', 27, 32) runtime we see the following error: Traceback (most recent call last): File "E:\tmp\tt.py", line 5, in <mod ule> func (' Rechar ') Typeerror:func () takes 2 positional arguments but 3 were given oh, Python will complain that we've passed too many parameters. And if it does, in some cases, we cannot guarantee that the number of arguments passed is equal to the number of arguments required by the function. This is the time to *iterable this argument, and if the definition of the function is defined, we add a parameter that starts with a "*", then this argument is actually a tuple type. If you pass more arguments than you need, those extra parameters will be put into this tuple. For example, Def func (name, age, *other): Print (name, age, and other) then, func (' Rechar ', 27, 32)
The output of this call is as follows: >>> Rechar 27 (32,)
Four, "**name" python in the definition of a function after loading all unmatched parameters that are not matched by name in the tuple of the parameter, what if there is an unmatched parameter matching by name? First look at the following example: Def func (name, age): Print (name, age) func (name = ' Rechar ', age =, pay= ' 1800 '), Python complains again: Traceba CK (most recent call last): File "E:\tmp\tt.py", line 5, at <module> func (name = ' Rechar ', age =, pay= ' 1800 ') Typeerror:func () got an unexpected keyword argument ' pay ' it says that func this function does not have a parameter named "Pay", which may occur after our function refactoring, the original function has this parameter. This function call may not have been modified elsewhere. Assuming that even given the "pay" parameter, the correctness of the program is not affected, yes, this is where the "**name" parameter comes in. If in the function definition, add a parameter to the function that starts with "* *", then this argument is actually a Dict object that puts all the arguments passed by name in the argument call into this dict. For example, Def func (name, Age,**other): Print (name, age, other) func (name = ' Rechar ', age =, pay= ' 1800 ')
Then run the results output, Rechar {' Pay ': ' 1800 '} See, the other here will not match the "pay= ' 1800" income bag.
V. Specify that calls must match by name when we define a function, if the first argument is a "*name" argument, it is conceivable that we cannot use sequential matching, because all the sequential parameter values will ultimately end up in the tuple. In order to pass values to subsequent parameters, we can only use a method that matches by name.
Six, "* *" parameters can only appear after the last formal parameter to think about why? It is well understood, because the formal parameters that appear after the "* *" parameter, whether used sequentially or by name, cannot ultimately reach the place where the parameter value really should be. So Python stipulates that if the "* *" argument is required, it must be the last formal parameter. Otherwise Python will report a syntax error.
Seven, "*" in the function call in the table we see a call with Func (*iteratable), which means that iteratable must be an iterative container, such as list, tuple, pass a value as a parameter, and when it finally passes to the function, Instead of appearing as a whole, you assign the elements of the element in order, one at a time, to the formal parameters of the function. For example, Li = [' Rechar ',] func (*li) This function call is equivalent to func (' Rechar ', 27).
Eight, the function call "*" Know "*" in the function call after the effect, also very good understanding of "* *" role. It will be passed in the Dict object decomposition, each element corresponding to a name passed by the parameter, according to the key to assign the parameter.