One. function names (scientific name: First Class object)
The function name is essentially the memory address of the function. Popular point is a special ordinary variable
def func (): Print (111) func () Print (func) # Results: # 111 # <function func at 0x00000150713f6048>
1. Can be referenced (that is, can be assigned to other variables)
def func (): Print (' infunc'= FUNCF ()# Result:# in func
2. Elements that can be used as container types
def func (): Print (111) def Func2 (): Print (222) def func3 (): Print (333= [Func (), Func2 (), func3 ()] for in li: i# Results: 111 222 333
3. Can be used as a function parameter and return value
def func (): Print (111) func () def Func2 (): return = Func2 () f ()# Result: run from top to bottom, call function after meeting func () to print a 111,# after 111 to F = Func2 (), call Func2 () to return to f a value of Func,# 111 last F () that is func () once again called after printing a 111
The first class of objects (first-class object) refers to
1. Can be created at run time
2. Can be used as function parameter or return value
3. Entities that can be stored in variables.
Two. Closed Package
Closure definition: An inner layer function, a reference to a variable of the outer layer function (non-global), called a closure
Use of closures: If your memory function is a closure, there is a mechanism inside python that encounters closures
He will open a memory space in memory and will not close with the end of the function
fromUrllib.requestImportUrlopenPrint(Urlopen ('http://www.cnblogs.com/jin-xin/articles/8259929.html'). Read ())defindex (URL): Content=urlopen (URL). Read ()defget (): With open ('crawler','a') as F1:f1.write (Content.decode ('Utf-8')) returnGetIndex ('http://www.cnblogs.com/jin-xin/articles/8259929.html') () index ('http://www.cnblogs.com/jin-xin/articles/8259929.html') () index ('http://www.cnblogs.com/jin-xin/articles/8259929.html') () index ('http://www.cnblogs.com/jin-xin/articles/8259929.html') () index ('http://www.cnblogs.com/jin-xin/articles/8259929.html')()defindex (): URL="http://www.xiaohua100.cn/index.html" defget ():returnurlopen (URL). Read ()returnGetcrawling Web pages (closures)
Determine if it is a closure function: __closure__
EF func (): Name='Alex' defFunc2 ():Print(name) Func2 ()Print(Func2.__closure__)#contains the cell is the closure None is not a closurefunc ()#Results:#Alex#(<cell at 0x0000011ad42a75b8:function object at 0x0000011ad6178a60>, <cell at 0x0000011ad42a75e8:str objec T at 0x0000011ad6177378>)name='Alex'deffunc ():defFunc2 ():Print(name) Func2 ()Print(Func2.__closure__)#None is not closuresfunc ()#Results:#Alex#Nonedeffunc (x):defFunc2 ():Print(x) Func2 ()Print(Func2.__closure__) func ('Alex')#Results:#Alex#(<cell at 0x0000029d98ce75b8:str object at 0x0000029d98d77538>)
Three. Perform intrinsic functions inside an outer function
def func (): def Func2 (): Print (111) Func2 () func()def func (): def Func2 (): Print (111) return Func2func () ()
Four. Decorative Device
For example, calculating the execution efficiency of a function
1. The simplest notation
Import Timedeffunc ():Print('We're all the same .') Start_time=time.time () time.sleep (0.1) func () End_time=time.time ()Print('execution efficiency of------%s'% (end_time-start_time))#Results:#We're all the same .#------Execution Efficiency 0.10034990310668945View Code
2. Advanced wording
import time def func (): print ( " We're all the same " ) def Timmer (): start_time = Time.time () time.sleep ( 0.1 Time.time () prin T ( " ------execution efficiency%s " % (End_time-start_time)) Timmer () # Although it can also run, it affects the normal operation of Func () and can also be advanced
View Code
3. Simple Decorative Device
Import Timedeffunc ():Print('We're all the same .')defTimmer (f):definner (): Start_time=time.time () time.sleep (0.1) F () End_time=time.time ()Print('execution efficiency of------%s'% (end_time-start_time)) returnInnerfunc=Timmer (func) func ()#can be understood as func,timmer,inner through assignment conversion, adding parameters, and finally only one func,#This allows you to run the program and to know the execution efficiencyView Code
4. Adorner with return value (Syntax sugar usage)
@timmer equivalent to #func = Timmer (func) is the name of the function to be asked for (the name of the letter to be asked)
Import TimedefTimmer (func):defInner (*args,**kwargs): Start_time=time.time () time.sleep (0.1) ret=func (*args,**kwargs) end_time=time.time ()Print('------Execution Efficiency%s'% (end_time-start_time)) returnretreturnInner@timmerdeffunc ():return 'If the sky is also old, the human path is vicissitudes'#func () cannot write this step if it prints the return value, otherwise the function will be called once, and the execution efficiency can be printed once more Print(func ())#Results:#------Execution Efficiency 0.1002659797668457#If the sky is also old, the human path is vicissitudes
5. General Purpose Decorators
def Wrapper (func): def Inner (*args,**Kwargs): " action before executing function "' = func ( *args,**Kwargs) ' operation after executing function '' return ret return Inner@wrapper def func (): Print (111) func ()
Python function three (adorner)