1. 程式人生 > >Python學習16--裝飾器1(簡單)

Python學習16--裝飾器1(簡單)

迭代器功能:

      不更改操作函式名的前提下,修改原函式功能!

      例如:

import time

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

@show_time #作用相當於foo = show_time(foo)
def foo():
    print('hello foo')
    time.sleep(3)
    


#foo = show_time(foo) 由於前面存在@show_time 所以可省
foo()