当前位置: 首页 > 图灵资讯 > 行业资讯> python操作带参的装饰器

python操作带参的装饰器

发布时间:2024-07-30 09:55:40

说明

1、装饰函数的第一个参数是装饰func,和以前一样。

2、另外一个参数timelimit是用位置参数编写的,具有默认值。

3、可变参数的写作方法和以前一样使用。

实例

fromdecoratorimportdecorator

@decorator
defwarn_slow(func,timelimit=60,*args,**kw):
t0=time.time()
result=func(*args,**kw)
dt=time.time()-t0
ifdt>timelimit:
logging.warn('%stook%dseconds',func.__name__,dt)
else:
logging.info('%stook%dseconds',func.__name__,dt)
returnresult

@warn_slow(timelimit=600#warnifittakesmorethan10minuteses
defrun_calculation(tempdir,outdir):
pass

以上是python操作带参装饰器的介绍,希望对大家有所帮助。更多Python学习指导:基础教程python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09