Parameter passing method of functions in Python
-General parameter Transfer
>>> def Add (a,b):
Return a+b
>>> print Add (1,2)
3
>>> print Add (' abc ', ' 123 ')
abc123 2. Optional parameter number, the parameter has the default value of the transfer
>>> def myjoin (string,sep= ' _ '):
return Sep.join (String)
>>> myjoin (' Test ')
' t_e_s_t '
>>> myjoin (' Test ', '; ')
' T;e;s;t ' >>> def myrange (Start=0,stop,step=1):
Print Stop,start,step
The default value of the Syntaxerror:non-default argument follows default argument parameter Sep is ' _ ' if this argument does not give a value, the default value is used if given, the given value is used
Note that if an argument is an optional parameter, then all the arguments behind it should be optional, and the order of the optional parameters can still be correctly assigned to the corresponding parameter but must clearly indicate the variable name and value
3. Variable number of parameters
>>> def printf (fmt,*arg):
Print Fmt%arg
>>> printf ('%d is larger than%d ', 2, 1)
The *arg in the 2 is larger than 1 function must be the last parameter, * represents any number of arguments, and *arg passes all parameters except the front to a function, which can be accessed by ARG in the function.
Arg is a tuple that can access Arg in a function by accessing the tuple method
Another way to pass any number of parameters is to pass the dictionary of the same way to accept multiple parameters, but each parameter needs to indicate a name corresponding relationship such as a=1,b=2,c=3
>>> def printf (Format,**keyword):
For k in Keyword.keys ():
print ' keyword[%s]%s%s '% (K,format,keyword[k])
>>> printf (' is ', one=1,tow=2,three=3)
Keyword[three] is 3
Keyword[tow] is 2
Keyword[one] is 1
When you define or invoke such a function, you follow the following rules:
1. arg=<value> must be followed by arg
2. *arg must be arg=<value> after
3. **arg must be *arg after
During a function call, the procedure for parameter assignment is this:
First, in order, the argument of "Arg" is given to the corresponding formal parameter.
Second, the "arg=<value>" form of the actual parameter assignment to the form
Third, the extra "arg" form of the argument is composed of a tuple to a formal parameter with an asterisk
Four, turn the extra "key=value" form of the argument into a dictionary to a formal parameter with two asterisks.
It sounds so complicated that it's actually very simple. Very intuitive, to see examples:
1. Def test (X,Y=5,*A,**B):
2. Print X,y,a,b
Just a simple function to see what happens when you face this 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 (1,2,y=1) ===> error, said Y gave multiple values
Test (1,2,3,4,a=1) ===> 1 2 (3,4) {' A ': 1}
Test (1,2,3,4,k=1,t=2,o=3) ===> 1 2 (3,4) {' K ': 1, ' t ': 2, ' O ': 3}
There are four main ways to define function parameters in Python:
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. For example:
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 two *, and the parameter is stored inside the function in dictionary in the form name identifier, when 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)