【相關學習推薦:#python教學】
# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapperdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 不用@ f = deco_test(do_something)("1","2","3")
輸出:
before function 1 2 3 after function
個人理解:
相當於在do_something
函數外面套了兩個輸出:before function
和after function
。
# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapper @deco_testdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
輸出:
before function 1 2 3 after function
個人理解:
##相當於執行do_something 函數的時候,因為有
@ 的原因,已經知道有一層裝飾器
deco_test,所以不需要再單獨寫
deco_test(do_something) 了。
# 带参数的装饰器def logging(level): def wrapper(func): def inner_wrapper(*args, **kwargs): print("[{level}]: enter function {func}()".format(level=level, func=func.__name__)) f = func(*args, **kwargs) print("after function: [{level}]: enter function {func}()".format(level=level, func=func.__name__)) return f return inner_wrapper return wrapper @logging(level="debug")def do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
#輸出:
[debug]: enter function do_something() 1 2 3 after function: [debug]: enter function do_something()
個人理解:
裝飾器帶了一個參數level = "debug"。
logging() 接受參數並將它們作用在內部的裝飾器函數上面。內層的函數
wrapper() 接受一個函數作為參數,然後在函數上面放置一個裝飾器。這裡的關鍵點是裝飾器是可以使用傳遞給
logging() 的參數的。
# 类装饰器class deco_cls(object): def __init__(self, func): self._func = func def __call__(self, *args, **kwargs): print("class decorator before function") f = self._func(*args, **kwargs) print("class decorator after function") return f @deco_clsdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
輸出:
class decorator before function 1 2 3 class decorator after function
個人理解:
使用一個裝飾器去包裝函數,傳回一個可呼叫的實例。因此定義了一個類別裝飾器。 兩層裝飾器# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapper# 带参数的装饰器def logging(level): def wrapper(func): def inner_wrapper(*args, **kwargs): print("[{level}]: enter function {func}()".format(level=level, func=func.__name__)) f = func(*args, **kwargs) print("after function: [{level}]: enter function {func}()".format(level=level, func=func.__name__)) return f return inner_wrapper return wrapper @logging(level="debug")@deco_testdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
輸出:
[debug]: enter function wrapper() before function 1 2 3 after function after function: [debug]: enter function wrapper()
個人理解:
在函數do_something() 外面先套一層
deco_test() 裝飾器,再在最外面套一層
logging() 裝飾器。
想了解更多程式設計學習,請關注php培訓欄位!
#
以上是聊聊Python裝飾器的詳細內容。更多資訊請關注PHP中文網其他相關文章!