zoukankan      html  css  js  c++  java
  • 装饰递归函数

    装饰递归函数:

    简洁版:
    import time
    
    def GetRunFunc(func):
        Call = True
        if Call:
            def Call_Func(*args, **kwargs):
                Call = False
                start_time = time.time()
                ret = func(*args, **kwargs)
                end_time = time.time()
                Run_time = end_time - start_time
                print(str(func.__name__) + "函数的运行时间为:" + str(Run_time) + "秒")
                return ret
    
        return Call_Func
    
    
    注释版:
    import time
    
    
    def GetRunFunc(func):
        Call = True
        if Call:
            # if Call: 此判断语句是针对装饰递归函数的情况, 如果装饰过此函数后, 就不再装饰
            def Call_Func(*args, **kwargs):
                Call = False
                start_time = time.time()
                # 开始时间
                ret = func(*args, **kwargs)
                # 调用被装饰的函数, func(*args, **kwargs), 调用函数为什么用func内, 装时器的特性, 将被装饰的函数的名字当作参数传递进来进行装饰
                end_time = time.time()
                # 结束时间
                Run_time = end_time - start_time
                # 运行时间 = 结束时间 - 开始时间
                print(str(func.__name__) + "函数的运行时间为:" + str(Run_time) + "秒")
                return ret
                # 将被装饰的函数的返回
    
        return Call_Func
        # 返回函数的引用
    
    
  • 相关阅读:
    python开发环境安装
    python文件I/O
    python字符串方法以及注释
    python列表
    php: Can't use function return value in write context
    Notice : brew install php70
    对web开发从业者的发展方向的思考
    关于微信跨号支付
    MySQL触发器写法
    MySQL慢查询日志
  • 原文地址:https://www.cnblogs.com/amou/p/8991699.html
Copyright © 2011-2022 走看看