1. Namespaces
A. Global namespace (namespace): When you run a program, the corresponding relationship of the memory address of the variable to the value, excluding the function.
B. Local namespace: In the case of a function, the function name exists in memory, regardless of the function body. The execution of a function temporarily opens up a space, storing the code inside the function body, outside the function to access the contents of the temporary space, after the function is executed, the temporary space will be released.
C. Built-in namespaces: space for Python-built content, such as print ()
D. Global scope: global namespace built-in namespace
Local scope: Local namespace
2. Load order: Built-in namespaces---Global namespaces (when program execution)---local namespaces (when function calls)
Order of values (one-way irreversible): The local namespace (when the function is called)---the global namespace (when the program executes)---the built-in namespace. The value is only a reference and cannot be changed
3. Nesting of functions: The following code executes after the function in the nested is finished. Functions in a function are different temporary spaces
- Global (): Returns a dictionary that holds the contents of the global namespace. Print (Globals ())
Locals (): Returns a dictionary, all variables in the current position, if multiple functions are nested within a function, only the layer of print (locals ()) where locals () is returned
5.gloabal: Referencing and changing a global variable; declaring a global variable at a local scope
Count = 1
Def func1 ():
Global Count #引用全局的count变量, if this variable is not available globally, you can also declare a new count here
Count = Count + 1 #可以在函数体内改变count的值, if there is no global, only count can be referenced here and cannot be changed
Count = Count + 100
Print (count)
Func1 ()
Print (count) #全局的count也改变
Noloacal: cannot manipulate global variables; the variable from which layer is referenced, all changes from that layer onwards
Def func1 ():
Count = 1
def inner ():
nonlocal Count #引用上一级的count
Count = Count + 3 #改变原count的值
Print (count) #在inner函数内改变后的值
Inner ()
Print (count) #上一级的count也改变, this count is a local variable and cannot be accessed globally
Func1 ()
6. Function name use (you can interpret the function name as a variable name):
A. Function name can be printed print (FUNC1) <function func1 at 0x000000000258f9d8>
B. Function name can be used as the element of container class data list = [Func1, Func2, func3]
C. The function name can be used as a function parameter Func2 (FUNC1) func1 equivalent to a variable name
D. Function name can be returned as a function return func1 return the func1 to the performer of the current function
7. Closure function
Definition: Inner layer function reference to non-global variable of outer layer function
Function: The closure function does not release with the end of the parent function
Def fun1 ():
name = ' CC '
Def inner1 ():
Print (name) #引用父级的name
Inner1 ()
Print (Inner1. Closure) #判断inner1是否是闭包, if returning none is not a closure function, if a memory address is returned it is a closure function
FUN1 ()
8. Decorative Device
Essence: Closure function
Function: Add some additional functions without affecting the execution of the original function
General Syntax:
Def Timmer (f):
def inner (*args,*Kwgrgs):
"' Operation before being decorated function '
ret = f (Args,**kwgrgs)
"' operation after being decorated function '
return ret
return inner
Python Fourth day Study summary