The essence of Function name
The function name is essentially the memory address of the function.
1, can be used as a value or variable reference
def func (): print (111) print (func) #<function func at 0x00000218a911ab70>--function in memory address F = Func # Pass it to the variable fprint (f) #<function func at 0x00000279a5a6ab70>f () #111
2. Functions can be stored as elements of a container type
#将其当做容器存储在列表中def func1 (): print (' func1 ') def func2 (): print (' Func2 ') def func3 (): print (' func3 ') lis = [ Func1, Func2, Func3]lis[0] () #func1lis [1] () #func2lis [2] () #func3
3, can be used as a function parameter and return value
#当做参数和返回值def func1 (): print (' func1 ') def func (argv): argv () return argv #func1作为返回值f = func (func1) #func1作为参数f () #输出func1func1
Summarize:
# 1. The memory address of the function name is--print (letter name). # 2, the function name can be assigned to other variables. # 3. The function name can be used as the element of the container class. # 4, the function name can be used as a function parameter. # 5, the function name can be used as the return value of the function. # Scientific Name: First Class objects (first-class object)
# In short: You can use the function name as a variable
Second, closed package
An inner function is defined in an outer function, and a temporary variable of the outer function is used in the inner function, and the return value of the outer function is a reference to the inner function. This makes up a closure (i.e., a reference to a variable of the inner layer function, the outer layer function (non-global)).
#闭包函数的基本用法def func (): name = ' Eva ' def Inner (): print (name) return innerf = Func () f () #eva
Common properties for detecting closure functions: __closure__
#闭包函数 # Output If a cell is the closure function Def wrapper (): name = ' is closure ' def inner (): print (name) #是闭包 inner () Print (inner.__closure__) # (<cell at 0x000001945ce07588:str object at 0x000001945ce06b70>,) wrapper () # Non-closure function # Output If None is not the closure function name = ' not closures ' Def wrapper (): def inner (): print (name) #不是闭包 inner () print (inner.__closure__) #Nonewrapper ()
Step two of the Python function