Python — Decorator
接續Python — Closure寫Closure的文章,這篇來寫寫關於Decorator的主題。
文章簡介
- Decorator背景介紹
- Decorator Constructor
- Decorator Factory
- Stacked Decorator
- Decorator的應用 — Memory Cache
- 補充 — 使用Decorator來Decorate class
Decorator背景介紹
白話來說,Decorator做到的事情就是包裝一個function,讓這個function除了執行原本的運算外,再額外的加上包裝的function,並且這個Decorator是可以重複被不同function拿來使用的,拿Closure文章的最後一個範例來看:
track_dictionary = {}#Decorator function
def count_function(fn):
count = 0
def count_inner_function(*args, **kwargs):
nonlocal count
count = count + 1
print(f"Frunction {fn.__name__} has been run {count} times")
track_dictionary[fn.__name__] = count
return fn(*args,**kwargs)
return count_inner_function#Def some functions without being decorated
def add(a, b):
return a + b
def mul(a, b):
return a * b
def factorial(n):
result = 1
for number in range(1, n+1):
result = result * number
return result#Running add_function 2 times#Decorate the add() function
add_function_counter = count_function(add)#Run the function after decorated
print(add_function_counter(4,5)) >> 9
>> Function add has been run 1 times
print(add_function_counter(5,5)) >> 10
>> Function add has been run 2 times
我們將原先的add function當成參數傳入count_function()中,最後return回來一個包裝過後的function — count_inner_function。
count_inner_function除了具備原先add function的功能外,還被包裝上了調用Free Variable count 來計次、print目前function被調用次數、以及將次數與匹配的function更新到track_dictionary的功能。
而關鍵的一步就是包裝的這一個過程:
#Decorate the add() function
add_function_counter = count_function(add)我們也可以依循Python的Decorator寫法,用@符號來完成包裝:
track_dictionary = {}#Decorator function
def count_function(fn):
count = 0
def count_inner_function(*args, **kwargs):
nonlocal count
count = count + 1
print(f"Frunction {fn.__name__} has been run {count} times")
track_dictionary[fn.__name__] = count
return fn(*args,**kwargs)
return count_inner_function#Decorate the function with decorator
@count_function
def add(a, b):
return a + b#直接調用add()
print(add(4,5))
>> Function add has been run 1 times
>> 9
print(add(5,5))
>> Function add has been run 2 times
>> 10
print(track_dictionary)
>> {'add': 2}
Decorator Constructor
整理一下關於Decorator的criteria:
- Decorator takes a function
- Decorator returns a Closure
- The returned Closure usually accepts any combination of parameters
- Runs some code inside the Closure
- The Closure function calls the original function using the arguments passed to the Closure
依循這些,我們可以建構出一個general的Decorator的程式樣板:
#Decorator
def decorator_function(func):
def wrapping_function(*args, **kwargs):
#do someting in the function
print("do something")
result = func(*args, **kwargs)
return result
return wrapping_function接著就可以套用這個Decorator到function上:
@decorator_function
def add_on(a, b):
return a + bDecorator Factory
這部分要寫的是我們可以在一般的Decorator外面再包一層Function,最後用這個外部function去return一個Decorator。
def outer(reps):
def decorator_function(func):
def wrapping_function(a, b):
result = func(a,b)
for _ in range(reps):
print(f"Hello, the result is {result}")
return wrapping_function
return decorator_function我們在原本的decorator_function外面再多加上了一層outer function,多疊加上這層function的user case是當面對需要額外的參數去做運算時,就可以使用這樣的方式去達到,例如上述的情況就是我額外多傳了一個Free Variable到最內層wrapping_function的for迴圈裏面,因為我想要根據我傳的參數去做不同次數的運算,而在文章裡面有說到,不管疊加多少層,最內層的Nested function都可以透過cell的Indirect Reference接收到值,這也是我體認到Free Vriable和Closure強大的地方。
因為最後這個outer function回傳的function是decorator,相當於我們呼叫outer(reps)時製造了一個Decorator出來,因此這種應用又稱為Decorator Factory(工廠)。
#outer(10) returns a decorator -> Decorator Factory@outer(10)
def adding(a,b):
return a+b#根據傳入的次數10次,去跑10次function
adding(3,5)
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
>> Hello, the result is 8
試著再疊加一層看看:
def outer_outer(years):
def outer(reps):
def decorator_function(func):
def wrapping_function(a, b):
result = func(a,b)
for _ in range(reps):
print(f"Hello, the result is {result}, {years}")
return wrapping_function
return decorator_function
return outer
@outer_outer(2022)(10)
def adding(a,b):
return a+b
adding(3,5)
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022
>> Hello, the result is 8, 2022Stacked Decorator
Stacked Decorator如其名,就是一個function去堆疊多個Decorators,讓被decorate function有次序的去執行Decoator function裡面寫的運算,最後才來執行被decorate的function本身:
from datetime import datetime
from time import perf_counter
from functools import reduce
#第一個Decorator, 紀錄function在何時執行
def logged(func):
def inner(*args, **kwargs):
now = datetime.now()
print(f"Current time is {now}, executing the function")
func(*args, **kwargs)
return inner
#第二個Decorator,紀錄function跑了多久
def time_consuming(function):
def processing(*args, **kwargs):
start_time = perf_counter()
function(*args, **kwargs)
end_time = perf_counter()
run_time = end_time - start_time
print(f"{function.__name__} ran for {run_time}.")
return run_time
return processing
#Stack Decorators - 一次使用兩個decorators,注意執行順序
@logged #->先執行
@time_consuming #->後執行
def func1(): #->最後執行
print("running func1")
func1()#執行結果
>> Current time is 2022-10-19 15:54:13.992089, executing the function
>> running func1
>> func1 ran for 4.199973773211241e-06
第一次看的時候,關於執行的次序可能有些困惑,但可以把Stack Decorators的過程看做:
final_func = logged(time_consuming(func1))我們在Evaluating function時是先由內而外,time_consuming(func1)得到的結果是processing,接著再把processing丟到logged()裡面。
Get John’s stories in your inbox
Join Medium for free to get updates from this writer.
而當我們執行final_func()執行順序是由外而內,依序是:
- Step1: 執行最外層的logged(),傳入的function是time_consuming(func1)的結果,也就第二層Decorator的回傳結果processing
- Step2: 計算datetime.now()
- Step3: print(“Current time is 2022–10–19 15:54:13.992089, executing the function”)
- Step4: 呼叫processing()
- Step5: 呼叫func1()
- Step6: print(“running func1”)
- Step7: 計算run_time
- Step8: print(“fun1 ran for 4.199973773211241e-06”)
- Step9: 回傳run_time
再次強調,使用Stack Decorator時的次序性,也就是執行先後非常重要,實際的user case像是當我們今天想要寫一支API供使用者利用,我們想要達到API被使用前先(1)驗證用戶是否為有權限的使用者(2)紀錄該名使用者使用API的這個行為。
這是我們可以使用Stack Decorator,只是我們要決定是
- 先驗證使用者,再記錄行為:
@auth #驗證使用者
@logged #紀錄行為
def using_API(): #執行
pass2. 先記錄行為,再驗證使用者:
@logged #記錄使用者
@auth #驗證行為
def using_API(): #執行
passDecorator的應用 — Memory Cache
這個應用應該已經在網上被廣泛的討論過,就是用Dynamic Programming的概念,這邊則是寫成Decorator來解。
先看要解決什麼樣的問題?我們想要使用recursion來解決費波那契數列,會寫成:(我加上一個print() function觀察跑的次序)
#跑fib(6)
def fib(n):
print(f"Calculating fib{n}")
return 1 if n < 3 else fib(n-2) + fib(n-1)print(fib(6))>> Calculating fib6
>> Calculating fib4
>> Calculating fib2
>> Calculating fib3
>> Calculating fib1
>> Calculating fib2
>> Calculating fib5
>> Calculating fib3
>> Calculating fib1
>> Calculating fib2
>> Calculating fib4
>> Calculating fib2
>> Calculating fib3
>> Calculating fib1
>> Calculating fib2
>> 8
使用圖片把這個function運算fib(6)的結果和過程表示出來,過程的部分就是一般的recursion,重點在於左下角每個fib()的運算次數,我們想要避免重複運算的情形(Ex: fib(4)已經在第一次fib(6) = fib(4) + fib(5)的時候運算過了、但在後面fib(5) = fib(3) + fib(4)時又被重複運算了一遍,導致整體的運算時間會提升。
我們可以試著使用Decorator去做到跑function之外,也額外的使用一個cache dictionary去紀錄已被運算fib(n)的值,這樣這個fib(n)下次再出現時,被可以直接看cahche dictionary的結果返回值。
#Using decorator to do recursion of fibonachi(6)
def memorize(func):
cache = dict()
def inner(n):
if n not in cache: #檢查這個值之前有沒有被算過
cache[n] = func(n) #沒有的話加到cache裡面
return cache[n] #之前被算過了,直接回傳值
return inner
@memorize
def fib(n):
return 1 if n < 3 else fib(n-1) + fib(n-2)print(fib(6))>> Calculating fib6
>> Calculating fib5
>> Calculating fib4
>> Calculating fib3
>> Calculating fib2
>> Calculating fib1
>> 8
而且這個cache dictionary因為是被Decorate Function的Free Variable的關係,重複跑依舊可以可以直接用查表的方式得到結果不需要重新計算:
print(fib(6))
print(fib(7))>> 8 #不用重複算
>> Calculating fib7 #只要算7
>> 13
來看一下使用cache前後的時間運算差異(計算fib(20)):
- Without cache
from time import perf_counter
def fib(n):
print(f"Calculating fib{n}")
return 1 if n < 3 else fib(n-2) + fib(n-1)
start_time = perf_counter()
fib(20)
end_time = perf_counter()
print(end_time-start_time)>> 0.18sec
- With Cache
from time import perf_counter
def memorize(func):
cache = dict()
def inner(n):
if n not in cache:
cache[n] = func(n)
return cache[n]
return inner
@memorize
def fib(n):
print(f"Calculating fib{n}")
return 1 if n < 3 else fib(n-1) + fib(n-2)
start_time = perf_counter()
fib(20)
end_time = perf_counter()
print(end_time-start_time)>> 0.00012530002277344465sec
補充 — 使用Decorator來Decorate class
Decorator除了Decorate funciton之外,也可以被用來Decorate class,只是就不會運用到Closure、Free Variable的概念,而是純粹修改原本的class內容,return新的被Decorate過的class:
Decorate class要使用的特性就是當class新增method或是attribute時,是可以直接透過賦值的方式來完成這一點:
class Test:
def __init__(self, a, b):
self.a = a
self.b = b#幫Test class加上新attribute和新的method
Test.c = 100
Test.new_method = lambda self: "This is a new method"test_obj = (1,2)
print(test_obj.c) >> 100
print(test_obj.new_method() >> This is a new method"
當我們將這一個特性使用Decorator來完成:
def dec_class(cls):
cls.c = 100
cls.new_method = lambda self: "This is a new method"
return cls #包裝過後的class
@dec_class
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
test_obj = Test(1,2)
print(test_obj.a) >> 原本construct時傳入的1
print(test_obj.b) >> 原本construct時傳入的2
print(test_obj.c) >> new class construct的100
print(test_obj.new_method()) >> new class construct的method






