There are two more aspects of the use of function parameters that are worth noting:
1. How function parameters are defined
2. How parameters are parsed during a function call
Let's look at the first question first. There are four main ways to define function parameters in python:
(arg1,arg2,...)
This is the most common way of defining a function, a function can be defined with any number of parameters, each parameter is separated by a comma. Functions defined in this way must be called with an equal number of values (actual parameters) in parentheses after the name of the function and in the same order, i.e., in this way of calling the function, the number of formal and real parameters must be the same, and they must be one-to-one, that is to say The first formal parameter corresponds to the first real parameter. For example:
def a(x,y):
print x,y
Call this function, a(1,2) then x takes 1, y takes 2, the form corresponds to the real parameter, if a(1) or a(1,2,3) will report an error.
(arg1,arg2=value2,...)
This approach is an improved version of the first, providing default values
def a(x,y=3):
print x,y
Call the function, a(1,2) or x to take 1, y to take 2, but if a(1), the error will not be reported, this time x is still 1, y for the default 3. These two ways, you can also replace the position of the parameter, for example, a(y = 8, x = 3) with this form is also possible.
(*arg1)
The first two ways are to pass in as many real parameters as there are formal parameters, but sometimes you're not sure how many parameters there are, so the third way is more useful, which is to add a * to the name of the formal parameter to indicate that the function has an indeterminate number of real parameters, which may be zero or n. Note that no matter how many there are, they are stored inside the function in a tuple identified by the formal parameter name.
>>> def a(*x):
if len(x)==0:
print 'None'
else:
print x
>>> a(1)
(1,) #Stored in tuples
>>> 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'
(**arg1)
Form parameter name before adding two * that the parameters in the function will be stored in the form of the name of the identifier in the dictionary, then the method of calling the function you need to use arg1=value1,arg2=value2 such a form.
>>> def a(**x):
if len(x)==0:
print 'None'
else:
print x
>>> a()
None
>>> a(x=1,y=2)
{'y': 2, 'x': 1} #stored in a dictionary
>>> a(1,2) #This call results in an error.
Traceback (most recent call last):
File "<pyshell#25>", line 1, in -toplevel-
a(1,2)
TypeError: a() takes exactly 0 arguments (2 given)
Above introduces the four definitions, the next look at the function parameters in the process of how to be resolved, in fact, as long as you remember the above four methods in order to reduce the priority of the first 1, after 2, then 3, and finally 4, that is, the first way 1 in the resolution of the arg, and then resolved in the way 2 in the way of the arg = value, and then resolved in the way of the way 3, that is, more than arg in this form of the real parameter composed of a tuple passed in, and finally the rest of the key=value this form of real parameters into a dictionary passed to the form with two asterisks, that is, way 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'
1. How function parameters are defined
2. How parameters are parsed during a function call
Let's look at the first question first. There are four main ways to define function parameters in python:
(arg1,arg2,...)
This is the most common way of defining a function, a function can be defined with any number of parameters, each parameter is separated by a comma. Functions defined in this way must be called with an equal number of values (actual parameters) in parentheses after the name of the function and in the same order, i.e., in this way of calling the function, the number of formal and real parameters must be the same, and they must be one-to-one, that is to say The first formal parameter corresponds to the first real parameter. For example:
def a(x,y):
print x,y
Call this function, a(1,2) then x takes 1, y takes 2, the form corresponds to the real parameter, if a(1) or a(1,2,3) will report an error.
(arg1,arg2=value2,...)
This approach is an improved version of the first, providing default values
def a(x,y=3):
print x,y
Call the function, a(1,2) or x to take 1, y to take 2, but if a(1), the error will not be reported, this time x is still 1, y for the default 3. These two ways, you can also replace the position of the parameter, for example, a(y = 8, x = 3) with this form is also possible.
(*arg1)
The first two ways are to pass in as many real parameters as there are formal parameters, but sometimes you're not sure how many parameters there are, so the third way is more useful, which is to add a * to the name of the formal parameter to indicate that the function has an indeterminate number of real parameters, which may be zero or n. Note that no matter how many there are, they are stored inside the function in a tuple identified by the formal parameter name.
>>> def a(*x):
if len(x)==0:
print 'None'
else:
print x
>>> a(1)
(1,) #Stored in tuples
>>> 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'
(**arg1)
Form parameter name before adding two * that the parameters in the function will be stored in the form of the name of the identifier in the dictionary, then the method of calling the function you need to use arg1=value1,arg2=value2 such a form.
>>> def a(**x):
if len(x)==0:
print 'None'
else:
print x
>>> a()
None
>>> a(x=1,y=2)
{'y': 2, 'x': 1} #stored in a dictionary
>>> a(1,2) #This call results in an error.
Traceback (most recent call last):
File "<pyshell#25>", line 1, in -toplevel-
a(1,2)
TypeError: a() takes exactly 0 arguments (2 given)
Above introduces the four definitions, the next look at the function parameters in the process of how to be resolved, in fact, as long as you remember the above four methods in order to reduce the priority of the first 1, after 2, then 3, and finally 4, that is, the first way 1 in the resolution of the arg, and then resolved in the way 2 in the way of the arg = value, and then resolved in the way of the way 3, that is, more than arg in this form of the real parameter composed of a tuple passed in, and finally the rest of the key=value this form of real parameters into a dictionary passed to the form with two asterisks, that is, way 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'