python 装饰器

2021-07-08 05:06

阅读:556

1、作用域 2、高阶函数 3、闭包

import time

def logger(flag=False):
def show_time(f):
def inner(*args,**kwargs):
start = time.time()
f(*args,**kwargs)
end = time.time()
print(end-start)
if flag:
print(‘record log...‘)
return inner
return show_time


@logger(True) # foo = show_time(foo)
def foo(a, b):
print(‘foo...%d‘ % (a+b))
time.sleep(2)


foo(2, 5)


评论


亲,登录后才可以留言!