1. 程式人生 > >Python學習16--裝飾器2(功能函式含引數)

Python學習16--裝飾器2(功能函式含引數)


import time

def show_time(func):
    def wrapper(*x):
        start_time = time.time()
        func(*x)
        end_time = time.time()
        print('spend %s' % (end_time - start_time))
    return wrapper

@show_time
def foo(*a):
    sums = 0
    for i in a:
        sums += i
    print(sums)
    time.sleep(3)

#foo = show_time(foo)
foo(1,2,3,4,5,6,7)#根據實際操作(簡化記憶難度)該語句執行後直接走到wrapper函式處