首页 > 编程语言 > 详细

python装饰器

时间:2019-02-17 19:59:58      阅读:257      评论:0      收藏: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.cnblogs.com/shiguoqiang/p/10392235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!