In python, function parameters are passed through "value assignment. However, this rule only answers the "strategic question" passed by function parameters, and does not answer the "tactical question", that is, it does not answer how to assign values.
. The use of function parameters can be divided into two aspects: how to define function parameters, and how to parse the parameters when a function is called. The latter is determined by the former. Function parameters can be defined in four forms.
Formula:
1. F (arg1, arg2 ,...)
2. F (arg2 = <value>, arg3 = <value> ...)
3. F (* arg1)
4. F (** arg1)
1st
One way is the most "traditional" method: A function can define unlimited number of parameters. parameters (formal parameters) are placed in parentheses following the function name, separated by commas. Function defined in this way
When a number is called, it must also provide the same number of values (actual parameters) in the parentheses after the function name. The order must be the same. That is to say, the number of form parameters and real parameters must be the same, and
The value of parameter 1 must be the first in the real parameter, and the relationship between the two parameters is one-to-one, that is, "parameter 1 = real parameter 1"
Parameter 2 = parameter 2 ...". Obviously, this is a very inflexible form. For example: "def addon (x, y): Return x +
Y ", the addon function defined here can be called in the form of addon (), which means that the parameter X will be set to 1 and the main parameter will be set to 2. Addon (1, 2, 3) and addon
(1) they are all incorrect.
The 2nd method is better than the 1st method, and the default value has been defined for each parameter. Therefore, when calling such a function, if the real parameter is not passed to the corresponding form parameter, then this form parameter will
Use the default value. For example, "def addon (x = 3, y = 5): Return x +
Y ", then the call form of addon (6, 5) indicates that the parameter X is set to 6, and the value of Y is set to 5. In addition, the addon (7) format is also possible, indicating that the X parameter is set to 7, and y takes the default value to 5. This
If you want X to take the default value, what should I do if I use the real parameter to assign a value to Y? The first two call Methods obviously do not work, so we need to use the function call method in Python.
-- Guan jianzi's assignment method. You can use addon (y = 6). In this case, the default value of X is 3, while that of Y is 6. In this way, you can perform "precise attack" on the form parameters by specifying the form parameters ",
The function of a secondary belt does not need to follow the order of the formal parameters, such as addon (y = 4, x = 6). This is also possible. This method of fixed-point value assignment using formal parameters is applicable to 1st
Method-defined functions are also applicable.
The number of parameters defined in the preceding two methods is fixed. For example, if five parameters are defined when a function is defined, therefore, a maximum of five real parameters can be passed to a call. However, in actual programming
You cannot always determine how many parameters a function has. The 3rd method is used to cope with this situation. It is represented by an asterisk (*) appended with the name of the form parameter. The actual parameters of this function are not certain. It can be zero or
It can be n. No matter how many, the function is stored in the tuple named identifier. For example:
The call to this function addon () addon (2) addon (3, 4, 5, 6) and so on are all possible.
Similar to the 3rd method, the form parameter name is preceded by two * representations, and the parameter is stored in a dictionary named identifier within the function. At this time, the call function must be in the form of key1 = value1, key2 = value2. For example:
1. Def addon (** Arg ):
2. Sum = 0
3. If Len (ARG) = 0: Return 0
4. Else:
5. For X in Arg. itervalues ():
6. Sum + = x
7. Return sum
You can call this function by using addon () or, for example, addon (x = 4, y = 5, K = 6.
The methods defined in the four function forms and their call methods are described separately. In fact, these four methods can be combined to form a complex and diverse form of Parameter definition. When defining or calling such a function, follow the following rules:
1. Arg = <value> must be after Arg
2. * Arg must be after Arg = <value>
3. ** Arg must be after * ARG
In the function call process, the process of assigning values to parameters is as follows:
First, the real parameters in the form of "Arg" are given to the corresponding form parameters in order.
Second, assign the real parameters in the form of "Arg = <value>" to the form
Third, a tuple is made up of multiple parameters in the form of "Arg" to form a parameter with an asterisk.
Fourth, convert the multiple arguments in the form of "Key = value" to a dictionary parameter with two asterisks.
It sounds complicated, but it is actually very simple. It is intuitive. Here is an example:
1. Def test (X, Y = 5, * a, ** B ):
2. Print X, Y, a, B
Let's look at the results of this simple function call:
Test (1) ==> 1 5 (){}
Test (1, 2) ==> 1 2 (){}
Test (1, 2, 3) ==> 1 2 (3 ,){}
Test (1, 2, 3, 4) ==> 1 2 (3, 4)
Test (x = 1) ==> 1 5 (){}
Test (x = 1, y = 1) ==> 1 1 (){}
Test (x = 1, y = 1, A = 1) ==> 1 1 () {'A': 1}
Test (x = 1, y = 1, A = 1, B = 1) => 1 1 () {'A': 1, 'B': 1}
Test (1, y = 1) ==> 1 1 (){}
Test (, y = 1) ==> error, that is, y assigned multiple values
Test (1, 2, 3, 4, A = 1) ==> 1 2 (3, 4) {'A': 1}
Test (, k = 1, t = 2, O = 3) ==> 1 2 () {'K': 1, 't': 2, 'o': 3}
Http://ssailyang.javaeye.com/blog/105298