Passing function parameters in python (parameters with asterisks are described)

Source: Internet
Author: User
Tags define function

The usage of function parameters is worth noting in two aspects:
1. How are function parameters defined?
2. How are parameters parsed during function call?

First, let's look at the first question. In python, there are four main methods to define function parameters:
1. F (arg1, arg2 ,...)
This is the most common definition method. A function can define any parameter. Each parameter is separated by a comma, 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, and the order must be the same, that is to say, in this call method, the number of form parameters and real parameters must be the same and must correspond one to one, that is, the first form parameter corresponds to the first real parameter. For example:
Def a (x, y ):
Print x, y
Call this function. If a () is used, x takes 1, y takes 2, and the form is used to correspond to the real parameters. If a (1) or a (, 3), an error is returned.

2. F (arg1, arg2 = value2 ,...)
This method is the first release version and provides the default value.
Def a (x, y = 3 ):
Print x, y
Call this function. a (1, 2) also returns x to take 1, y to take 2, but if a (1), no error will be reported. At this time, x is still 1, y is the default value of 3. The above two methods can also change the parameter location, such as a (y = 8, x = 3.

3. F (* arg1)
The preceding two methods are as follows, it adds a * parameter name to indicate that the number of real parameters of this function is not fixed. It may be 0 or n. Note that no matter how many parameters are stored in the function, they are stored in the tuple named identifier.
>>> Def a (* x ):
If len (x) = 0:
Print 'none'
Else:
Print x
>>> A (1)
(1,) # store in tuples
>>> ()
None
>>> A (1, 2, 3)
(1, 2, 3)
>>> A (m = 1, y = 2, z = 3)

Traceback (most recent call last ):
File "<pyshell #16>", line 1, in-toplevel-
A (m = 1, y = 2, z = 3)
TypeError: a () got an unexpected keyword argument 'M'

4. F (** arg1)
The first two parameters * are used to indicate that the parameters are stored in the dictionary in the form of identifiers. In this case, arg1 = value1 must be used to call the function, arg2 = value2.
>>> Def a (** x ):
If len (x) = 0:
Print 'none'
Else:
Print x
>>> ()
None
>>> A (x = 1, y = 2)
{'Y': 2, 'x': 1} # store in the dictionary
>>> A (1, 2) # An error is returned for such a call.

Traceback (most recent call last ):
File "<pyshell #25>", line 1, in-toplevel-
A (1, 2)
TypeError: a () takes exactly 0 arguments (2 given)

The above describes four definition methods. Next, let's take a look at how function parameters are parsed during the call process. In fact, as long as you remember that the priority of the above four methods is reduced in turn, first 1, then 2, 3, 4, that is, first parse the arg in method 1, then the arg = value in method 2, and then the parsing method 3, that is to say, the extra parameters in the form of arg are grouped into tuple and passed to the form of real parameters in the form of key = value in the form of a dictionary to form parameters with two asterisks, that is, Method 4.
>>> Def test (x, y = 1, * a, ** B ):
Print x, y, a, B

>>> Test (1)
1 1 (){}
>>> Test (1, 2)
1 2 (){}
>>> Test (1, 2, 3)
1 2 (3 ,){}
>>> Test (1, 2, 3, 4)
1 2 (3, 4 ){}
>>> Test (x = 1, y = 2)
1 2 (){}
>>> Test (1, a = 2)
1 1 () {'A': 2}
>>> Test (1, 2, 3, a = 4)
1 2 (3,) {'A': 4}
>>> Test (1, 2, 3, y = 4)

Traceback (most recent call last ):
File "<pyshell #52>", line 1, in-toplevel-
Test (1, 2, 3, y = 4)
TypeError: test () got multiple values for keyword argument 'y'

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.