示例1(a)
執行結果:import time
defbar()
: time.sleep(
0.1)
('in the bar'
)def
test
(func)
:#不改變bar的源**的情況下新增功能
start_time = time.time(
) func(
) stop_time = time.time(
('執行時間為:{}'
.format
(stop_time-start_time)
('func:{}'
.format
(func)
)#func==bar
test(bar)
#但是修改的呼叫方式
('bar:{}'
.format
(bar)
)
示例2(b)in the bar
執行時間為:
0.100982666015625
func:
>
bar:
>
執行結果:import time
defbar()
: time.sleep(
0.1)
('in the bar'
)def
test
(func)
(func)
#新增了新功能
return func
bar = test(bar)
bar(
)#沒有改變呼叫方式
ps:所有的鋪墊是為了學習裝飾器>
in the bar
高階函式 裝飾器
1,高階函式 裝飾器 閉包 1 存在於巢狀關係的函式中 2 巢狀函式引用外部函式變數 3 外部函式會將內部函式名作為返回值返回 1 裝飾器是乙個閉包 2 執行過程 3 外部函式執行後並沒有 要被裝飾的函式,沒有引數,沒有返回值 def outer func print 開始裝飾 def inner ...
函式高階(裝飾器)
python裝飾器本質上就是乙個函式,它可以讓其他函式在不需要做任何 變動的前提下增加額外的功能,裝飾器的返回值也是乙個函式物件 函式的指標 裝飾器函式的外部函式傳入我要裝飾的函式名字,返回經過修飾後函式的名字 內層函式 閉包 負責修飾被修飾函式。從上面這段描述中我們需要記住裝飾器的幾點屬性,以便後...
Python高階函式 裝飾器
由於函式也是乙個物件,而且函式物件可能被賦值給變數,所以,通過變數也能呼叫該函式。def now print 2018 4 11 f now f 2018 4 11 函式物件有乙個 name 屬性,可以拿到函式的名字 now.name now f.name now 現在,假設我們要增強now 函式的...