美文网首页
python中的装饰器

python中的装饰器

作者: 写出你我 | 来源:发表于2018-11-22 16:24 被阅读0次

设计一个decorator装饰器,它可作用于任何函数上,并打印该函数的执行时间:

import time

def deco(func):

    def wrapper(*args, **kw):

        start_time = time.time()

        func(*args, **kw)

        end_time = time.time()

        print('the function %s runed time is %s' % (func.__name__, (end_time - start_time)))

    return wrapper

@deco

def test():

    time.sleep(2)

    print('this is test func')

test()

相关文章

网友评论

      本文标题:python中的装饰器

      本文链接:https://www.haomeiwen.com/subject/zgilqqtx.html