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()

猜你喜欢

转载自blog.csdn.net/qq_33661910/article/details/81259264