For Python functions, we need to remember that:
1. The default return value of the function is none.
2. Python is a language that is interpreted and executed from top to bottom. Therefore, the definition of the function must precede the function being called. A function with the same name, later defined, overrides the previously defined.
3. when the program executes, the function definition will only read the whole function into memory and not execute it immediately. The function body is not executed until the function is called.
4. The parameters of the Python function pass a reference instead of a value. This is important. This can be demonstrated by the following example:
def F1 (a_list): = ID (a_list) = 5 print(s) print= [1,2,3,4,]f1 (LI) print(li)print(LI)
Operation Result:
11797576[5, 2, 3, 4]11797576[5, 2, 3, 4]
For a function, the most important point of knowledge is the parameter. The parameters in Python are as follows:
First, the general parameters:
Also called positional parameters, they are fixed in the argument list, collectively on the leftmost side, and must be passed according to their order. For example:
def F1 (a,b,c): Print (a,b,c) F1 (a)
for each parameter, when the function reference, we can specify the parameter , this is a method of passing the value of a parameter. In this way, the argument can be out of order, but it must be in the last part of the parameter sequence. For example:
def F1 (a,b,c): Print (a,b,c) F1 (1,c=3,b=2)
but that doesn't work:
def F1 (a,b,c): Print (a,b,c) F1 (c=3,1,b=2)
Second, the default parameters:
The default parameter is to set a default value for some parameters, either to reduce the number of incoming parameters or to enjoy the convenience of using the default values. The default parameter must be in the last part of the parameter list! For example:
def F1 (a,b,c=3): print(a,b,c) F1 (up)
The above example can also be quoted as follows: F1 (1,2,4), when we specify a value of C of 4 instead of using the default value.
However, in the mixed use of common parameters and default parameters to pay attention to a problem, the following example, A, B is a normal parameter, C, D is the default parameter. We want C to use the default value, but D to specify the value in addition. The correct approach is to:
def F1 (a,b,c=3,d=4): print(a,b,c,d) F1 (1,2,d=8)
Here to explicitly use the "d=8" delivery form. such as F1 (1,2,,8), F1 (1,2,8) and other ways are not.
Three, dynamic parameters
Python has two dynamic parameters, namely *args and **kwargs, the key is a and two asterisks, not args and Kwargs, you can actually use *any or **whatever way, but like self, the unspoken rules we use * Args and **kwargs.
Python function parsing