Adorner:
Definition: The essence is a function, function: (Decorate other functions) is to add additional functions for other functions;
Principle: 1, cannot modify the source code of the adornment function
2. Cannot modify the calling mode of the decorated function
Implement the Adorner Knowledge Reserve:
1, function is "variable"
2. High-order function
3. Nested functions
Higher order function + nested function = "Adorner"
Import timedef Timer (func): def warper (*args,**kwargs): start_time =time.time () func () stop_time= Time.time () print (' The Func run time is%s '% (stop_time-start_time)) return warper@timerdef test1 (): Time.sleep (3) print (' in the Test1 ') test1 ()
anonymous function: No Name required (memory recycle)
Calc = Lambda x:x*3print (Calc (3))
#函数即 "Variable" # def foo (): # print (' In the Foo ') # bar () # foo () #----------------------def bar (): print (' in the bar def foo (): print (' In the Foo ') bar () foo () #-----------------------------------------------def foo (): Print (' In the Foo ') Bar () def bar (): print (' in the Bar ') foo ()
The path to Python learning: the prelude to adorners