# The transfer of function parameters under Python (parameter with an asterisk description)

Source: Internet
Author: User
Tags define function
The use of function parameters has two important aspects to note:
1. How the function parameters are defined
2. How parameters are parsed in the process of calling a function

First of all, there are four main ways to define function parameters in Python:
1.F (arg1,arg2,...)
This is the most common way of defining, a function can define any parameter, each parameter is separated by commas, the function defined in this way must also be in the parentheses after the function name to provide an equal number of values (the actual parameters), and the order must be the same, that is, in this method of invocation, The number of formal parameters and arguments must be the same, and must be one by one corresponding, that is, the first parameter corresponds to this first argument. For example
def a (x, y):
Print x, y
Call this function, a (to) x takes 1,y to take 2, the shape participates in the argument corresponds, if a (1) or a (X-ray) will be an error.

2.F (arg1,arg2=value2,...)
This is the first improved version, which provides the default value
def a (x,y=3):
Print x, y
Call this function, a (1,Y) is also x to take 2, but if a (1), then no error, this time X or 1,y is the default of 3. In both of these ways, you can also change the parameter position, such as a (y=8,x=3) in this form is also possible.

3.F (*ARG1)
The two ways are the number of parameters, the number of arguments passed in, but sometimes it is not sure how many arguments, then the third way is more useful, it is a * plus formal parameter name of the way to represent the function of the variable number of arguments, it may be 0 or maybe n. Note that, regardless of how many, inside the function are stored in a tuple with the parameter name identifier.
>>> def A (*x):
If Len (x) ==0:
print ' None '
Else
Print X
>>> A (1)
(1,) #存放在元组中
>>> A ()
None
>>> A (A/b)
(1, 2, 3)
>>> A (m=1,y=2,z=3)

Traceback (most recent):
File " ", Line 1, in-toplevel-
A (m=1,y=2,z=3)
Typeerror:a () got an unexpected keyword argument ' m '

4.F (**ARG1)
The parameter name plus two * means that the parameters inside the function will be stored in the form named Identifier dictionary, the method of calling the function needs to take the form of arg1=value1,arg2=value2.
>>> def A (**x):
If Len (x) ==0:
print ' None '
Else
Print X
>>> A ()
None
>>> A (x=1,y=2)
{' Y ': 2, ' X ': 1} #存放在字典中
>>> A (#这种调用则报错)

Traceback (most recent):
File " ", Line 1, in-toplevel-
A (+)
Typeerror:a () takes exactly 0 arguments (2 given)

Described above four kinds of definitions, then see how the function parameters in the call process is resolved, in fact, as long as the above four methods to remember the priority of the lower, first 1, 2, then 3, the last 4, that is, the way 1 in the ARG parsing, and then parse the way 2 in the Arg=value, and then parse the way 3, That is, the more out of the arg form a tuple, and finally the remainder of the Key=value form a dictionary to the formal parameters with two asterisks, also the way 4.
>>> def Test (x,y=1,*a,**b):
Print X,y,a,b

>>> Test (1)
1 1 () {}
>>> Test (+)
1 2 () {}
>>> Test (all in all)
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):
File " ", line 1, in-toplevel-
Test (1,2,3,y=4)
Typeerror:test () got multiple values for keyword argument ' y '
• Related Keywords:
Related Article

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.