python在带参的函数中使用装饰器
小妮浅浅
2021-08-19 09:29:485079浏览 · 0收藏 · 0评论

方法说明
1、如果要包装的函数有参数,需要内嵌包装函数的形参和返回值与原函数相同。
2、装饰函数返回内嵌包装函数对象。
实例
import datetime,time
def out(func):
def inner(*args):
start = datetime.datetime.now()
func(*args)
end = datetime.datetime.now()
print(end-start)
print("out and inner")
return inner
@out
def myfunc(*args):
time.sleep(1)
print("args is{}".format(args))
myfunc("lalalal")以上就是python在带参函数中使用装饰器的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
关注公众号,随时随地在线学习
相关文章