Passing function parameters in Python

Source: Internet
Author: User
Tags define function

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 the question of 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:

1. F (arg1, arg2 ,...)
2. F (arg2 = , Arg3 = ...)
3. F (* arg1)
4. F (** arg1)

the 1st method is the most "traditional" method: A function can define unlimited number of parameters. parameters (formal parameters) are placed in parentheses following the function name, parameters are separated by commas. When calling a function defined in this way, you must 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 the form parameter and the real parameter must be the same, and the value given to the form parameter 1 must be the first in the real parameter, and the relationship between the form and the real parameter must be one-to-one, that is, "parameter 1 = 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 (), meaning that the parameter X will be set to 1, the master value is 2. Both addon (, 3) and addon (1) are incorrect.
the 2nd methods are better than the 1st methods. The default values have been defined for each form parameter. Therefore, if real parameters are not passed to the corresponding formal parameters when calling such a function, the default value is used for this parameter. For example, if "def addon (x = 3, y = 5): Return X + Y", 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. At this time, there will be a problem. What should I do if I want X to take the default value and assign a value to y using the real parameter? The first two call forms obviously do not work. In this case, another trick to use the function call method in python is the Guan jianzi 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. A sub-band function does not need to follow the order of the form parameters, for example: addon (y = 4, x = 6. This method of fixed-point value assignment using formal parameters is also applicable to functions defined in the 1st method.
the number of parameters defined in the preceding two methods is fixed. For example, if five parameters are defined during function definition, 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 necessarily, either zero or 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 = Must be after Arg
2. * Arg must be in Arg = After
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, set "Arg = "This form of real arguments is assigned to 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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.