Python裝飾器(decorator)學習
點選(此處)摺疊或開啟
-
#!/usr/bin/env python
-
#coding=utf8
-
import sys
-
-
def deco(func):
-
print("before %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
func()
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return func
-
-
def deco_at_one(func):
-
print("before %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
func()
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return func
-
-
def deco_at_inline_noparams(func):
-
def _deco_inline():
-
print("before %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
res = func()
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return res
-
return _deco_inline
-
-
def deco_at_inline_params_fix(func):
-
def _deco_inline_params_fix(a, b):
-
print("before %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
res = func(a, b)
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return res
-
return _deco_inline_params_fix
-
-
def deco_at_inline_params_unfix(func):
-
def _deco_at_inline_params_unfix(*args, **kwargs):
-
print("before %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
res = func(*args, **kwargs)
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return res
-
return _deco_at_inline_params_unfix
-
-
def deco_at_inline_params_unfix_withparams(dcargs):
-
def _deco_at_inline_params_unfix_wp(func):
-
def __deco_at_inline_params_unfix_wp(*args, **kwargs):
-
print("before %s() called on %s(%s)." % (func, sys._getframe().f_code.co_name, dcargs))
-
res = func(*args, **kwargs)
-
print("after %s() called on %s(%s)." % (func, sys._getframe().f_code.co_name, dcargs))
-
return res
-
return __deco_at_inline_params_unfix_wp
-
return _deco_at_inline_params_unfix_wp
-
-
-
def deco_at_inline_params_unfix_wp_cls(dc_cls):
-
# dc_cls must implement static method function 'fn_sm_1' and 'fn_sm_2'
-
# see 'locker' module
-
def _deco_at_inline_params_unfix_wp_cls(func):
-
def __deco_at_inline_params_unfix_wp_cls(*args, **kwargs):
-
print("before %s() called on %s(%s)." % (func, sys._getframe().f_code.co_name, dc_cls))
-
dc_cls.fn_sm_1()
-
res = func(*args, **kwargs)
-
dc_cls.fn_sm_2()
-
print("after %s() called on %s." % (func, sys._getframe().f_code.co_name))
-
return res
-
return __deco_at_inline_params_unfix_wp_cls
-
return _deco_at_inline_params_unfix_wp_cls
-
-
class Class_Deco():
-
def __init__(self):
-
print("INIT deco class." )
-
-
@staticmethod
-
def fn_sm_1():
-
print(" call %s." % (sys._getframe().f_code.co_name))
-
-
@staticmethod
-
def fn_sm_2():
-
print(" call %s." % (sys._getframe().f_code.co_name))
-
-
'''
-
-------------------------------------------------------------------------------------------------------------------
-
'''
-
def fn():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
def fn_deco():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
'''
-
decorator function 'deco_at_one' called only one time before 'main'
-
decorator function 'deco_at_one' not called when per time call 'fn_at_deco_one'
-
'''
-
@deco_at_one
-
def fn_at_deco_one():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
'''
-
decorator function 'deco_at_inline_noparams' called when per time call 'fn_at_deco_inline'
-
'''
-
@deco_at_inline_noparams
-
def fn_at_deco_inline_noparams():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
'''
-
params samed between '_deco_inline_params_fix' and 'fn_at_deco_inline'
-
'''
-
@deco_at_inline_params_fix
-
def fn_at_deco_inline_params_fix(a,b):
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return a+ b
-
-
'''
-
'deco_at_inline_params_unfix' any params
-
'''
-
@deco_at_inline_params_unfix
-
def fn_at_deco_inline_params_unfix_2(a,b):
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return a+ b
-
-
'''
-
'deco_at_inline_params_unfix' any params
-
'''
-
@deco_at_inline_params_unfix
-
def fn_at_deco_inline_params_unfix_3(a,b,c):
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return a + b + c
-
-
'''
-
function 'deco_at_inline_params_unfix_withparams' can input params
-
'''
-
@deco_at_inline_params_unfix_withparams("wpa")
-
def fn_at_deco_inline_params_unfix_wp_a(a,b,c):
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return a + b + c
-
-
@deco_at_inline_params_unfix_withparams("wpb")
-
def fn_at_deco_inline_params_unfix_wp_b(a,b,c):
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return a + b * c
-
-
@deco_at_inline_params_unfix_wp_cls(Class_Deco)
-
def fn_at_deco_inline_params_unfix_wp_cls():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
'''
-
execute order by @deco_[1,2,3]
-
@deco_1
-
@deco_2
-
@deco_3
-
def fn():
-
pass
-
-
'''
-
@deco_at_inline_noparams
-
@deco_at_inline_params_unfix_withparams("wpc")
-
@deco_at_inline_params_unfix_wp_cls(Class_Deco)
-
def fn_at_deco_inline_mix():
-
print("%s() called." % (sys._getframe().f_code.co_name))
-
return 'ok'
-
-
if __name__ == '__main__':
-
lin = " - " * 30
-
-
print lin
-
fna = fn()
-
-
print lin
-
fnb = deco(fn_deco)
-
-
print lin
-
fnc = fn_at_deco_one()
-
fnc = fn_at_deco_one()
-
-
print lin
-
fnd = fn_at_deco_inline_noparams()
-
fnd = fn_at_deco_inline_noparams()
-
-
print lin
-
a=2
-
b=3
-
fne = fn_at_deco_inline_params_fix(a,b)
-
print a,b,fne
-
-
a=12
-
b=32
-
fne = fn_at_deco_inline_params_fix(a,b)
-
print a,b,fne
-
-
print lin
-
a=2
-
b=3
-
fnf = fn_at_deco_inline_params_unfix_2(a,b)
-
print a,b,fnf
-
-
a=2
-
b=3
-
c=5
-
fnf = fn_at_deco_inline_params_unfix_3(a,b,c)
-
print a,b,c,fnf
-
-
print lin
-
fng = fn_at_deco_inline_params_unfix_wp_a(a,b,c)
-
print a,b,c,fng
-
fng = fn_at_deco_inline_params_unfix_wp_b(a,b,c)
-
print a,b,c,fng
-
-
print lin
-
fng = fn_at_deco_inline_params_unfix_wp_cls()
-
-
print lin
-
fnh = fn_at_deco_inline_mix()
-
- exit(0)
參考 : http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html
其他參考資料
1. Python裝飾器學習 http://blog.csdn.net/thy38/article/details/4471421
2. Python裝飾器與面向切面程式設計 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
3. Python裝飾器的理解
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-1816298/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python 之裝飾器(decorator)Python
- python裝飾器decorator的應用Python
- 學習 ES7 語法 decorator 裝飾器
- 裝飾器模式(Decorator)模式
- decorator-裝飾器
- python 裝飾器小白學習Python
- python函式修飾器(decorator)Python函式
- 裝飾器vue-property-decoratorVue
- JavaScript中的裝飾器--DecoratorJavaScript
- Python學習筆記 - 裝飾器Python筆記
- python基礎學習12—-裝飾器Python
- 裝飾模式 (Decorator Pattern)模式
- PHP設計模式- Decorator 裝飾器模式PHP設計模式
- ES6裝飾器Decorator基本用法
- 搭建node服務(四):Decorator裝飾器
- 前端常用設計模式(1)--裝飾器(decorator)前端設計模式
- quarkus依賴注入之八:裝飾器(Decorator)依賴注入
- 設計模式之裝飾器模式(decorator pattern)設計模式
- PHP設計模式之裝飾器模式(Decorator)PHP設計模式
- Decorator裝飾設計模式設計模式
- Python 中級學習之函式裝飾器Python函式
- C#設計模式-裝飾器模式(Decorator Pattern)C#設計模式
- python裝飾器2:類裝飾器Python
- 裝飾器學習筆記筆記
- Python 裝飾器初學筆記Python筆記
- Python 裝飾器Python
- Python裝飾器Python
- 設計模式--裝飾模式(Decorator Pattern)設計模式
- 如何用 Decorator 裝飾你的 TypescriptTypeScript
- 設計模式-裝飾模式(Decorator Pattern)設計模式
- 從零開始的Python學習Episode 11——裝飾器Python
- Python學習之路26-函式裝飾器和閉包Python函式
- 1.5.3 Python裝飾器Python
- Python 裝飾器(一)Python
- python的裝飾器Python
- Python 裝飾器原理Python
- Python裝飾器模式Python模式
- Python學習之三大名器-裝飾器、迭代器、生成器Python
- 黃金搭檔 -- JS 裝飾器(Decorator)與Node.js路由Node.js路由