In python, function parameters are passed through the assignment type. You must pay attention to the following two aspects when using functions: 1. function parameter definition process, 2. how to parse function parameters during the call process. The function calling process in python is divided into four methods. Here we will talk about five methods first, and the fifth method has been mentioned in previous articles.
1. function definitions for Parameter Enumeration:
>>> Def Fun (a, B, c): return (a, B, c) >>> Fun (1, 2, 3) (1, 2, 3) >>> fun () # The number of enumeration parameters does not correspond to Traceback (most recent call last): File "<pyshell #61>", line 1, in <module> Fun () TypeError: fun () takes exactly 3 arguments (2 given) >>>
>>> Def Fun (a, B, c): print (a, B, c) >>> Fun (a = 22, B = 33, c = 44) (22, 33, 44) >>> Fun (, 44) # If no value assignment object is specified, it is matched in order (22, 33, 44) >>> Fun, c = 44) # You can specify the parameter value object or not, (22, 33, 44) >>> Fun (B = 22, a = 33, c = 44) # The Parameter order can be different (33, 22, 44) >>>
This is the most common definition method. A function can enumerate and define any number of parameters. Each parameter is separated by a comma, when calling a function defined in this way, the number of real parameters must be equal to the number of actual parameters, and the order must be one-to-one, if the number of parameters is incorrect or the parameter type does not match, an error is returned.
2. function definitions with default parameters (note this before ):
>>> Def Fun (a, B, c = 33): print (a, B, c) >>> Fun (11, 22) # You can leave the default parameter unspecified (11, 22, 33) >>> Fun (1) # Traceback (most recent call last): File "<pyshell #66> ", line 1, in <module> Fun (1) TypeError: Fun () takes at least 2 arguments (1 given) >>> Fun (1, 2, 3) # override default parameters (1, 2, 3)
In this way, you can leave the default parameter unspecified during the call, but note that the function with the default value has initialized its default parameter during the definition, this is also discussed in previous articles.
3. function definitions with uncertain number of 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) # Traceback (most recent call last) cannot be assigned in dictionary mode ): file "<pyshell #93>", line 1, in <module> Fun (a = 1, B = 2, c = 33) TypeError: Fun () got an unexpected keyword argument 'A'> Fun () # It can be zero parameter None
The number of parameters with the "*" symbol is uncertain. The number of parameters to be passed during the call can be [0, n, however, all input parameters are placed in
In tuple, if you accept the returned values, tuple rows are used. If you assign values to the uncertain number of values in the function, it is OK to assign values to the loop iteration directly.
4. Definition of dictionary parameters with uncertain numbers:
Add two parameters * to the defined parameters, that is, when processing functions in the function, they are saved in the dictionary form. Of course, key = value is used for calling.
>>> Def Fun (** a): if 0 = len (a): print 'none' else: print a >>> Fun (1, 2, 4) # assign values to the dictionary, so Traceback (most recent call last): File "<pyshell #83>", line 1, in <module> Fun (,) TypeError: fun () takes exactly 0 arguments (4 given) >>> Fun (a = 1, B = 2, c = 3) # The key = value must be assigned {'A ': 1, 'C': 3, 'B': 2 }# dict type >>>> Fun (aa = 3, B = 333, d = 44) {'A ': 3, 'B': 333, 'D': 44}
The last example is about the priority assignment of function parameters:
>>> Def Fun (a, B, c = 22, * d, ** e): print a, B, c, d, e >>> Fun (1) traceback (most recent call last): File "<pyshell #98>", line 1, in <module> Fun (1) TypeError: Fun () takes at least 2 arguments (1 given) >>> Fun (1, 2) 1 2 22 () {}>> Fun (1, 2, 3) 1 2 3 () {}>>> Fun (, 5) # by default, the redundant parameters are matched to tuple1 2 3 (4, 5) {}>> Fun, 3, 4, 5, aa = 33, bb = 33) # assign the dictionary value to dict1 2 3 (4, 5) {'A': 33, 'bb ': 33 }>>> Fun (a = 3, c = 4, B = 2b) SyntaxError: invalid syntax >>> Fun (a = 3, c = 4, B = 22) # When assigning values in the dictionary and enumeration match values, priority should be given to assigning values 3 22 4 () {}>> Fun (B = 22, a = 22, e =, 3) 22 22 22 () {'E': (1, 2, 3) }>>> Fun (B = 22, c = 77,2, 6 ,) # SyntaxError: non-keyword arg after keyword arg >>> Fun (B = 22, c = 77,2, 3,4, k = 44) SyntaxError: non-keyword arg after keyword arg >>> Fun (B = 22, c = 77, a =,) >>> Fun (, 3, (, 3334, 34) # use the real parameters of tuple to pass
1 2 3 (22,333 4, 34 ),){}
It can be seen that the value assignment method of function parameters is different here. The priority values of 1, 2, 3, and 4 are reduced in sequence, that is, the enumerated parameters are assigned values first, and then the default parameters are assigned values, then all the extra values are assembled and passed in tuple, to the parameters with *, and finally assembled in the form of key = value into a dictionary and passed in to the parameters *.
5. the modifier's function call method (not the function definition method, but a function call Convention ):
@Fun
For details about this function call method, refer to the previous article: