1. Namespaces
1. Global namespace. When a variable outside the function is stored, the relationship (ID) of the variable name and value is stored in memory, which is called the global namespace.
2. Local namespaces. function, he will open a temporary civil space in memory, storing all variables and values in the function body.
As the function executes, the temporary space is closed and the function is used to open up the local namespace
3. Built-in namespaces, where all built-in functions are stored
Load order of three spaces:
Built-in namespace---"Global namespace" (when the function executes) loads the local namespace
Order of Values:
Local namespace--"global namespace---" Built-in load space (when function executes)
4. Global scope: Contains the global namespace, the variable effective scope of the built-in space
Local scope: The scope of the variables that are valid within the function body when the function is opened
2. Nesting of functions:
The relationship between a global variable and a local variable should be taken into account when a function is nested in a function.
def OK (): def RK (): print (' 22222 ') print (' 11111 ') return rk () #当没有return rk () The function inside the function does not execute
Ok ()
##############################################################
def OK ():
Def LK ():
Def PL ():
Print (' Nihao ')
Return PL ()
Print (LK ())
Output Result:
Nihao
None
None
3.globals () #全局名称空间: Querying all global variables
Locals () #局部名称空间: Querying all local variables
4. Keywords:
Global:1. Within a local space, declare a global variable
2. Change a global variable within a local space
Nonlocal:1. Make global variables non-modifiable
2. Ability to modify local variables, in local variables, modify all of the variables in one
Python function advanced