• 技术文章 >Python技术 >Python基础教程

    python操作带参的装饰器

    小妮浅浅小妮浅浅2021-07-29 09:39:08原创1923

    说明

    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电脑。

    专题推荐:python装饰器
    品易云
    上一篇:python模块如何安装 下一篇:python Axes容器如何绘图

    相关文章推荐

    • 九步就可入门Python装饰器• 一个例子解释python装饰器• python装饰器如何实现• python装饰器转换方法的注意点• python装饰器管理函数和类的注意点• Python装饰器的应用场景

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网