The function parameters in Python are transitive, and the use of functions should be noted in two aspects: 1. function parameter definition process, 2. How the function arguments are parsed during the call. First of all, let's say that the function call process in Python is in four ways, here and five, and the fifth is already mentioned in previous articles.
1. Function definition of parameter enumeration:
>>> def Fun (a,b,c): Return
(a,b,c)
>>> Fun (1,2,3)
(1, 2, 3)
>>> Fun (1,2) # The number of enumerated parameters does not correspond to
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
Fun (1,2) C8/>typeerror:fun () takes exactly 3 arguments (2 given)
>>>
>>> def Fun (a,b,c):
print (a,b,c)
>>> Fun (a=22,b=33,c=44)
(at)
> >> Fun (22,33,44) <span style= "White-space:pre" > </span># does not specify an assignment object, which is matched sequentially
(22, 33, 44)
>>> Fun (22,33,c=44) <span style= "White-space:pre" > </span># can specify an assignment object for the parameter. can also not specify,
(Fun,)
>>> (b=22,a=33,c=44) # parameter order can not correspond
(at)
>>>
This is the most common way to define a function that can enumerate any number of arguments. Each parameter is separated by commas, and functions defined in this way must have equal number of arguments and the number of formal parameters when invoked, and the order is one by one, and an error occurs if the number of arguments is incorrect or the parameter type is incorrect.
2. Function definitions with default parameters (previously written specifically in this area should be noted):
>>> def Fun (a,b,c=33):
print (a,b,c)
>>> Fun (11,22) # can not write default parameters
(one, one)
>> > Fun (1) # is at least the number of arguments and the number of non-default parameters is the same
Traceback (most recent call last):
File "<pyshell#66>", Line 1, in & Lt;module>
Fun (1)
Typeerror:fun () takes at least 2 arguments (1 given)
>>> Fun (1,2,3) <span style= "White-space:pre" > </span># can override default parameters
(1, 2, 3)
This way you can not fill out the default parameters at the time of the call, but note that the function with the default value has already initialized his default parameters when it is defined, as discussed in previous articles.
3. function definition of the number of indeterminate parameters:
>>> def Fun (*a):
If 0 = Len (a):
print ' None '
else:
print a
>>> Fun (1,2,3)
( 1, 2, 3) # tuple type
>>> Fun (a=1,b=2,c=33) # cannot be assigned in a dictionary
traceback (most recent call last): C11/>file "<pyshell#93>", line 1, in <module>
Fun (a=1,b=2,c=33)
typeerror:fun () got Unexpected keyword argument ' a '
>>> Fun () # can be 0 parameters
None
The number of the description parameters of the belt * is uncertain, and the number of parameters to be passed in the call is OK [0,n], oh, cool, but his incoming parameters will be placed in a
Tuple, if there is a return value of the time is also used tuple row, for the function of the indeterminate number of assignments directly on the cyclic iteration assignment is OK.