ITPub博客

首页 > IT职业 > IT职场 > python操作带参的装饰器

python操作带参的装饰器

IT职场 作者:cjk 时间:2021-09-11 09:54:18 0 删除 编辑

说明

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

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

3、和原来一样使用了可变参数的写法。

实例

from decorator import decorator
 
@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    t0 = time.time()
    result = func(*args, **kw)
    dt = time.time() - t0
    if dt > timelimit:
        logging.warn('%s took %d seconds', func.__name__, dt)
    else:
        logging.info('%s took %d seconds', func.__name__, dt)
    return result
  
@warn_slow(timelimit=600)  # warn if it takes more than 10 minutes
def run_calculation(tempdir, outdir):
    pass

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

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4715/viewspace-2829794/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: php高并发处理
请登录后发表评论 登录
全部评论

注册时间:2019-04-29

  • 博文量
    181
  • 访问量
    1130