Python多個裝飾器的順序
更新:多個裝飾器的呼叫順序是自下往上,但是執行時的執行順序是自上往下!!!
原文連結:http://www.cnblogs.com/nisen/p/6193426.html?utm_source=itdadao&utm_medium=referral
裝飾器是Python用於封裝函式或程式碼的工具,網上可以搜到很多文章可以學習,我在這裡要討論的是多個裝飾器執行順序的一個迷思。
疑問
大部分涉及多個裝飾器裝飾的函式呼叫順序時都會說明它們是自上而下的,比如下面這個例子:
def decorator_a(func):
print 'Get in decorator_a'
def inner_a(*args, **kwargs):
print 'Get in inner_a'
return func(*args, **kwargs)
return inner_a
def decorator_b(func):
print 'Get in decorator_b'
def inner_b(*args, **kwargs):
print 'Get in inner_b'
return func(*args, **kwargs)
return inner_b
@decorator_b
@decorator_a
def f(x):
print 'Get in f'
return x * 2
f(1)
上面程式碼先定義裡兩個函式: decotator_a,
decotator_b
, 這兩個函式實現的功能是,接收一個函式作為引數然後返回建立的另一個函式,在這個建立的函式裡呼叫接收的函式(文字比程式碼繞人)。最後定義的函式 f
採用上面定義的 decotator_a,
decotator_b
作為裝飾函式。在當我們以1為引數呼叫裝飾後的函式 f
後, decotator_a,
decotator_b
的順序是什麼呢(這裡為了表示函式執行的先後順序,採用列印輸出的方式來檢視函式的執行順序)?
如果不假思索根據自下而上的原則來判斷地話,先執行 decorator_a
再執行 decorator_b
,
那麼會先輸出 Get
in decotator_a
, Get
in inner_a
再輸出 Get
in decotator_b
, Get
in inner_b
。然而事實並非如此。
實際上執行的結果如下:
Get in decorator_a
Get in decorator_b
Get in inner_b
Get in inner_a
Get in f
函式和函式呼叫的區別
為什麼是先執行 inner_b
再執行 inner_a
呢?為了徹底看清上面的問題,得先分清兩個概念:函式和函式呼叫。上面的例子中 f
稱之為函式, f(1)
稱之為函式呼叫,後者是對前者傳入引數進行求值的結果。在Python中函式也是一個物件,所以 f
是指代一個函式物件,它的值是函式本身, f(1)
是對函式的呼叫,它的值是呼叫的結果,這裡的定義下 f(1)
的值2。同樣地,拿上面的 decorator_a
函式來說,它返回的是個函式物件 inner_a
,這個函式物件是它內部定義的。在 inner_a
裡呼叫了函式 func
,將 func
的呼叫結果作為值返回。
裝飾器函式在被裝飾函式定義好後立即執行
其次得理清的一個問題是,當裝飾器裝飾一個函式時,究竟發生了什麼。現在簡化我們的例子,假設是下面這樣的:
def decorator_a(func):
print 'Get in decorator_a'
def inner_a(*args, **kwargs):
print 'Get in inner_a'
return func(*args, **kwargs)
return inner_a
@decorator_a
def f(x):
print 'Get in f'
return x * 2
正如很多介紹裝飾器的文章裡所說:
@decorator_a
def f(x):
print 'Get in f'
return x * 2
# 相當於
def f(x):
print 'Get in f'
return x * 2
f = decorator_a(f)
所以,當直譯器執行這段程式碼時, decorator_a
已經呼叫了,它以函式 f
作為引數,
返回它內部生成的一個函式,所以此後 f
指代的是 decorater_a
裡面返回的 inner_a
。所以當以後呼叫 f
時,實際上相當於呼叫 inner_a
,傳給 f
的引數會傳給 inner_a
,
在呼叫 inner_a
時會把接收到的引數傳給 inner_a
裡的 func
即 f
,最後返回的是 f
呼叫的值,所以在最外面看起來就像直接再呼叫 f
一樣。
疑問的解釋
當理清上面兩方面概念時,就可以清楚地看清最原始的例子中發生了什麼。
當直譯器執行下面這段程式碼時,實際上按照從下到上的順序已經依次呼叫了 decorator_a
和 decorator_b
,這是會輸出對應的 Get
in decorator_a
和 Get
in decorator_b
。 這時候 f
已經相當於 decorator_b
裡的 inner_b
。但因為 f
並沒有被呼叫,所以 inner_b
並沒有呼叫,依次類推 inner_b
內部的 inner_a
也沒有呼叫,所以 Get
in inner_a
和 Get
in inner_b
也不會被輸出。
@decorator_b
@decorator_a
def f(x):
print 'Get in f'
return x * 2
然後最後一行當我們對 f
傳入引數1進行呼叫時, inner_b
被呼叫了,它會先列印 Get
in inner_b
,然後在 inner_b
內部呼叫了 inner_a
所以會再列印 Get
in inner_a
, 然後再 inner_a
內部呼叫的原來的 f
,
並且將結果作為最終的返回。這時候你該知道為什麼輸出結果會是那樣,以及對裝飾器執行順序實際發生了什麼有一定了解了吧。
當我們在上面的例子最後一行 f
的呼叫去掉,放到repl裡演示,也能很自然地看出順序問題:
➜ test git:(master) ✗ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test13
Get in decorator_a
Get in decorator_b
>>> test13.f(1)
Get in inner_b
Get in inner_a
Get in f
2
>>> test13.f(2)
Get in inner_b
Get in inner_a
Get in f
4
>>>
在實際應用的場景中,當我們採用上面的方式寫了兩個裝飾方法比如先驗證有沒有登入 @login_required
,
再驗證許可權夠不夠時 @permision_allowed
時,我們採用下面的順序來裝飾函式:
@login_required
@permision_allowed
def f()
# Do something
return
參考資料
- 我的大腦和好奇心