1 definition and invocation of function 1.1 function definition
1.2 Invocation of functions
2 function parameters
>>> def fun (A, B, *args, * *Kwargs): ..."""variable Parameter Demonstration example""".. print"A =", a ... print"B =", b ... print"args =", args ... print"Kwargs:"... forKey, Valueinchkwargs.items (): .... print key,"=", Value ...>>> Fun (1,2,3,4,5, m=6, n=7, p=8) # Note that the passed parameters correspond to a=1b=2args= (3,4,5) Kwargs:p=8m=6N=7>>>>>>>>>>>> C = (3,4,5)>>> d = {"m":6,"N":7,"P":8}>>> Fun (1,2, *c, * *d) # Note the way a tuple and dictionary are passed=1b=2args= (3,4,5) Kwargs:p=8m=6N=7>>>>>>>>>>>> Fun (1,2, C, D) # Note the difference between the asterisk and the above=1b=2args= ((3,4,5), {'P':8,'m':6,'N':7}) Kwargs:>>>>>>
3 function return value
4 local variables and global variables
Local variables
Global variables
Summarize:
- Variables defined outside the function are called
全局变量
- Global variables can be accessed in all functions
- If you modify a global variable in a function, you need to
global declare it, or else an error
- If the name of the global variable is the same as the name of the local variable, then the local variable is used, and the tip
强龙不压地头蛇
Summary 2:
- The nature of global variables cannot be modified when global variables are declared without using global in a function, which means that global variables cannot be modified to point to new data. lists and dictionaries can be declared without Globa.
- For global variables of an immutable type, global variables cannot be modified without using global because the data they point to cannot be modified.
- For variable-type global variables, global variables can also be modified without using global because the data they point to can be modified.
5 Anonymous functions
python-Basics-Functions