裝飾器1(被裝飾函數不帶參數)
阿新 • • 發佈:2017-10-01
test code 裏的 如果 pri 地址 cnblogs wrapper log
#!/usr/bin/env python # -*- coding:utf-8 -*- import time def wrapper(func): def deco(): start = time.time() func() stop = time.time() print("the func run %s"%(stop - start)) return deco #這裏的【@wrapper】等於【test1 = wrapper(test1)】,wrapper(test1),將test1函數以實參的形式傳遞給wrapper函數,wrapper函數將會把deco函數的內存地址作為返回值返回給變量test1,而此時test1變量所指引的值相當於是deco函數的內存地址,如果test1()則將會執行deco函數 @wrapperdef test1(): time.sleep(3) print("in the test1...") @wrapper def test2(): time.sleep(3) print("in the test2...") test1() test2()
裝飾器1(被裝飾函數不帶參數)