说明
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)#warnifittakesmorethan10minutes defrun_calculation(tempdir,outdir): pass
以上就是python操作带参装饰器的介绍,希望对大家有所帮助。
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。