创新互联Python教程:python操作带参的装饰器

说明

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了仁化免费建站欢迎大家使用!

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操作带参的装饰器
网站URL:http://gydahua.com/article/cdjheeh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流