1. Basic syntax for defining methods
def fun (N,m,...)
....
....
....
(return N)
About return:
1,return can have, can not,
2, no Return method returns None,
3,return back no expression also returns none,
4, the function cannot reach the end and returns none.
Cases:
def test (A, B):
C=a+b
There is no return on the function, so input test, no value returned.
def test (A, B):
C=a+b
Return C
The upper function adds a return, enter test
2. Parameter definition and variable parameters of functions in Python
A function in Python can have no arguments, or it can be made up of multiple parameters.
1. Conditions without parameters:
Def Funca ():
Pass
Obviously, the function Funca has no parameters (at the same time do nothing: D).
2. Two parameters:
The following function FUNCB has two parameters,
Def FUNCB (A, B):
Print a
Print B
At the time of invocation, we need to use the function name, plus a list of arguments that are expanded in parentheses, such as FUNCB (100, 99), and the result is:
100
99
Obviously, the order and number of parameters are consistent with the function definition, and if you execute FUNCB, Python will error:
TYPEERROR:FUNCB () takes exactly 2 arguments (1 given)
3. Formal parameters: the order in which parameters are disturbed when using a function
In the above example, when the function is called, the parameters passed are matched to the parameter table in the function definition according to the position, such as FUNCB (100, 99) and FUNCB (99, 100), the execution result is not the same. In Python, there is also a way to call a function using the keyword argument (keyword argument), which is to explicitly specify the parameter value to pay the argument when calling the function. For example, FUNCB (A, b) above, we call FUNCB (a=100, b=99) and FUNCB (b=99, a=100) in either of these ways
The result is the same as FUNCB (100, 99), because when we use the keyword parameter invocation, we specify the assignment of the value of 100 to the a,99 to B. In other words, the keyword argument allows us to disrupt the order of the arguments passed when calling the function!
In addition, in a function call, you can mix parameters based on location matching and keyword parameters, the first problem is to give the fixed position of the parameters, such as
Def funce (A, B, c):
Print a
Print B
Print C
The result of calling Funce (100, 99, 98) and calling Funce (c=98, b=99) is the same.
4. Setting default values for parameters
We can use the default values of the parameters in the function definition, such as
Def FUNCC (A, b=0):
Print a
Print B
In the definition of function FUNCC, parameter B has a default value, is an optional parameter, and if we call FUNCC, B automatically assigns a value of 0.
5. Inheritance of default values for parameters
The following program will appear strange:
Def f (A, l=[]):
L.append (a)
Returnl
Print (f (1))
Print (f (2))
Print (f (3))
Results:
[1]
[1, 2]
[1, 2, 3] #结果有继承性, because L set the default value
However, when the default value is not set:
Def f (A, L):
L.append (a)
Returnl
Print (f (1))
Print (f (2))
Print (f (3))
Results:
[1]
[2]
[3] #结果没有继承性, because L did not set the default value
It can be explained in fact, because Python is an explanatory language, unlike C + +, which compiles one side and prepares things, before executing, the function becomes a very fixed thing. But Python is not the same, it is explanatory, the program executes to the function definition of the local system to register the function in the table, and the default value of the parameter is assigned, so you can use the variable to assign a value. After the first use, we know that L is the element inside the function, and once again it is the same as the previous definition code in copy. Be aware of the explanatory language, because variables other than the default parameter values (including parameters) need to be re-assigned, so this does not show any difference, but the default parameter values, which may inherit the last data. (In fact, other variables will inherit, but they will be re-assigned before a single use, except that parameters with default values are special.) It is often not assigned, so that the last data is saved. Simply put: A couple of successive calls to a function are like repeating the contents of the function several times, and their variables have inherited attributes, except that the value of the parameter with the default value inherits the last data.
6. Parameters with *: used to accept variable quantity parameters
so far, when we're defining a function, we have to pre-define how many arguments (or how many arguments can be accepted) the function needs. In general, this is fine, but there is also the case when defining a function that does not know the number of arguments (think of the printf function in c), in Python, the parameter with * is used to accept a variable number of parameters. See an example
Def FUNCD (A, B, *c):
Print a
Print B
Print "Length of C is:%d"% Len (c)
Print C
Call FUNCD (1, 2, 3, 4, 5, 6) results are
1
2
Length of C is:4
(3, 4, 5, 6)
As we can see, the first two parameters are accepted by a, B, and the remaining 4 parameters are all accepted by C, and C is a tuple here. When we call FUNCD, we have to pass at least 2 parameters, 2 or more parameters, put in C, if there are only two parameters, then C is an empty tuple.
Func (*args)
The passed in parameter is in the form of a tuple in args, such as:
Func (**kwargs)
The arguments passed in are in the form of a dictionary in args, such as:
Func (*args, **kwargs)
The order of incoming must be the same as the order of the definition, here is the indefinite parameter list, and then the keyword parameter dictionary, such as:
7. Parameters with * *:
If the last formal parameter in a function definition has a * * (double star) prefix, all other keyword parameters other than the normal parameter will be placed in a dictionary passed to the function, such as:
Def FUNCF (A, **b):
Print a
For x in B:
Print x + ":" + str (b[x])
Call FUNCF (c= ' Hello ', b=200), execute the result
100
C: Hello
b:200
As you can see, B is a Dict object instance that accepts the keyword arguments B and c.
8. The most essential meanings of * and * *:
In Python functions, we have to pay attention to two features, one on the keyword parameters, and one is arbitrary parameters. The essential meaning of arbitrary parameters is to allow the function to accept any number of parameters, its essential meaning is to use *args to represent a set, and when this parameter is assigned, it will receive as much as possible the parameters it can accept to form a set.
That is, in fact, assuming that the function's formal parameter is corresponding to the number of arguments, and the introduction of *args, which represents *args, represents 0 to any number of parameters. Similarly **args (he is a dictionary collection). Other words
def fun (n, *args):
is a function definition is actually def fun (n,args[0],args[1],args[2] ...). To the inside of the function *args is a collection. That is to say, this symbol has the meaning of splitting , and this meaning has become a function in another place.
A collection is split into separate sets of parameters, rather than a set of elements. Here it is clear that the set is an object (no matter how many elements are inside), but rather a number of objects are split with * .
Found no, in fact, args is expressed as a set, plus *, it represents a number of elements.
In fact: def fun (N,*args):
This is true if you revert to a normal function:
def fun (N,args):
This is the time to use it:
Fun (["Xu", "Yong", "Quan"])
*args, when used as a parameter of a row, represents the split of this set into several variable elements. This is:
def fun (n, args[0],args[1],args[2] ...).
*args is also the meaning of splitting when it is used as an argument (variable).
Reference Link: http://blog.sina.com.cn/s/blog_6babbcb801018412.html
Http://blog.sina.com.cn/s/blog_93b45b0f0100z3et.html
Http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944416.html
http://blog.csdn.net/xmnathan/article/details/39156697
function variable parameter transfer value (Python)