Passing of a python function parameter (description with an asterisk) the tuple passes the dictionary to pass __ function

Source: Internet
Author: User
Tags define function

The transfer of function parameters in Python is passed by assignment. The use of function parameters has two aspects to note: 1. How the function parameter is defined 2. How the parameters are parsed during the invocation of a function

First of all, in Python, there are four main ways to define function parameters:
1.F (Arg1,arg2,...)
This is the most common way to define a function can define any parameter, each parameter is separated by commas, the function defined in this way must also provide the number of equal values (actual parameters) in the parentheses after the function name, and the order must be the same, that is, in this invocation method, 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 the first argument. e.g.
def a (x,y):
Print X,y
Call the function, a (1,2) x takes 1,y 2, the form participates in the argument, if a (1) or a (1,2,3) will error.


2.F (arg1,arg2=value2,...)
This is the first improved version that provides a default value
def a (x,y=3):
Print X,y
Call the function, a (1,2) is also x take 1,y 2, but if a (1), it will not be an error, this time X or 1,y is the default 3. The above two ways, but also can replace the parameter location, such as a (y=8,x=3) in this form is also possible.


3.F (*ARG1)
How many formal parameters are there in the above two ways, how many arguments to pass in, but sometimes uncertain how many parameters, then the third way is more useful, it is a * plus the formal parameter name of the way to indicate that the function of the variable number of arguments, may be 0 or N. Note that, no matter how many, the function is stored inside the tuple with the parameter name identifier.
>>> def A (*x):
If Len (x) ==0:
print ' None '
Else
Print X
>>> A (1)
(1,) #存放在元组中
>>> A ()
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 formal parameter name is represented by the preceding two * representations, where the parameter is stored inside the function in a dictionary with the parameter name identifier, and 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 (1,2) #这种调用则报错

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


The four definitions are described above, followed by how the function arguments are parsed during the call. In fact, as long as the above four methods to remember the priority of the lower order, first 1, after 2, then 3, the last 4, that is, the first 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 Arg this form of the argument into a tuple, and finally the remaining Key=value this form of the argument into a dictionary to take two asterisks of the formal parameters, also in Mode 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.