python 装饰器
2021-01-14 21:12
标签:print div import pre rpe __name__ timer pen name python 装饰器 标签:print div import pre rpe __name__ timer pen name 原文地址:https://www.cnblogs.com/jkklearn/p/12939657.html#函数返回值+1
def make(func):
def wrapper(*args,**kwds):
return func(*args,**kwds) + 1
return wrapper
@make
def add():
return 4
print add() #输出5
#函数返回值+N
def makearg(num):
def make(func):
def wrapper(*args,**kwds):
return func(*args,**kwds) + num
return wrapper
return make
@makearg(num=1)
def add(n):
return n + 4
print add(4) # 输出9
装饰器传参 参考 https://www.cnblogs.com/serpent/p/9445592.html#检测函数执行时间
import time
import sys
def timer(func):
def wrapper(*args,**kwds):
start=time.time()
func(*args,**kwds)
end=time.time()
print "exe function %s take %s time" %(func.__name__,str(end - start))
return wrapper
@timer
def add():
print "start"
time.sleep(5)
print "ending"
add()