装饰器:功能函数加参数

无参:
import time
def show_time(f): #装饰器函数
def inner(): #闭包函数
stat = time.time()
f()
end = time.time()
print('spend %s' % (end - stat))
return inner
@show_time #相当于foo = show_time(foo)
def foo() : #原始函数
print('foo....')
time.sleep(2)
foo()


有参:
import  time
def show_time(f): #装饰器函数
def inner(*x,**y): #闭包函数
stat = time.time()
f(*x,**y)
end = time.time()
print('spend %s' % (end - stat))
return inner
@show_time #foo = show_time(foo)
def foo(*a,**b):
sums = 0
for i in a:
sums += i
print(sums)
time.sleep(1)
foo(1,2,3,4)
posted @ 2019-07-08 13:06  python小白丶  阅读(145)  评论(0编辑  收藏  举报