Function name (first object)
1. Memory address of function, print (func)
def func (): Print (222) Print (func)
2. Function names can be assigned to other variables
def func (): Print (222= funcprint(f ())
3. Function names can be used as elements of a container class
def func1 (): Print (111) def Func2 (): Print (222) def func3 (): Print (333= [func1,func2,func3] for in Li: i ()
4. Function names can be used as arguments to functions
def func1 (): Print (111) def Func2 (x): x () Func2 (func1)
5. Function names can be used as return values of functions
def F1 (): Print ('F1') def func1 (argv): argv () return= func1 (F1) F ()
Closures: (cell)
1. Inner layer function, reference to outer layer function (non-global) variable, called closure
def wrapper (): ' Alex ' def inner (): Print (name) inner () print(inner. __closure__) # Check if the closure return= Wrapperf ()
#输出的__closure__有cell元素: is the closure function
' Egon ' def Func2 (): def inner (): Print (name) Print (Inner. __closure__ ) return= Func2 () F2 ()
#输出的__closure__为None: Not a closure function
2. Internal execution of the outer layer function inner ()
def wrapper (): ' Alex ' def inner (): Print (name) inner () print(inner. __closure__) # Check if the closure return= wrapperf ()
Benefits of Closures : If the memory function is a closure, Python has a mechanism inside it that encounters a closure that opens a memory space in memory and does not close as the function ends.
Adorner: 1, simple adorner
Import timedef func1 (): print('infunc1') def Timer (func): def Inner (): = time.time () func () Print (Time.time ()- start) return = timer (func1) func1 ()
View Code
2, adorner with parameters
Import Time def Timer (func): def Inner (a): = Time.time () func (a) print(Time.time ()- start) return Inner@timer def Func1 (a): Print (a) func1 (1)
View Code
3, adorner with return value
4, Universal Decorator
Import TimedefTimer (func):defInner (*args,**Kwargs): Start=time.time () re= Func (*args,**Kwargs)Print(Time.time ()-start)returnRereturnInner@timer#==> func1 = Timer (func1)defFunc1 (A, b):Print('In func1') @timer#==> Func2 = Timer (FUNC2)defFunc2 (a):Print('In Func2 and get a:%s'%(a))return 'fun2 over'Func1 ('aaaaaa','bbbbbb')Print(Func2 ('aaaaaa') ) Adorner---the adorner holding all parameters
View Code
5, Grammar sugar @
Import Time def Timer (func): def inner (): = time.time () func ()print(Time.time ()- start) return Inner@timer # ==> func1 = Timer (func1) def func1 (): Print ('infunc1') func1 () Adorner ---syntax sugar
Open closure principle: 1. Open to extensions, why open to extensions?
We say that no one program can have all the features in mind at the beginning of the design and will not make any updates or changes in the future. So we have to allow code extensions, add new features.
2. If the modification is closed, why should the amendment be closed?
As we have just mentioned, as we write a function that is likely to have been delivered to someone else, if we modify it at this time, it is likely to affect other users who are already using the function.
The adorner perfectly follows this open closure principle.
The main function and fixed structure of the adorner
# Fixed Format def Wrapper (func): def Inner (*args,**Kwargs): " action before executing function "' = Func (*args,**kwargs) ' operation after executing function '' return ret return Inner def func (): Print (666)
Multiple adorners decorate a function
defouter (flag):defTimer (func):defInner (*args,**Kwargs):ifflag:Print(" "what to do before executing a function" ") ret= Func (*args,**Kwargs)ifflag:Print(" "what to do after the function is executed" ") returnretreturnInnerreturnTimer@outer (False)deffunc ():Print(111) func ()
View Code
Python's path-function (Advanced)