zoukankan      html  css  js  c++  java
  • Python学习之路:装饰器实现

    import  time
    
    def timer(func):#timer(test1) func=test1
        def deco():
            start_time=time.time()
            func()#run test1
            stop_time=time.time()
            print('the func run time is %s'%(stop_time-start_time))
        return deco
    
    def test1():
        time.sleep(3)
        print('in the test1')
    
    def test2():
        time.sleep(3)
        print('in the test2')
    
    print(timer(test1))
    test1=timer(test1)
    test1()#----->deco
    
    
    #-------------------------------------------------------------------
    import  time
    
    def timer(func):#timer(test1) func=test1
        def deco():
            start_time=time.time()
            func()#run test1
            stop_time=time.time()
            print('the func run time is %s'%(stop_time-start_time))
        return deco
    
    @timer #加装饰器 test1=timer(test1)
    def test1():
        time.sleep(3)
        print('in the test1')
    
    @timer #加装饰器
    def test2():
        time.sleep(3)
        print('in the test2')
    
    test1()
    test2()
    
  • 相关阅读:
    安装wamp的方法及过程
    js原生获取className&多选一
    构造函数
    轮播图
    NaN
    ++与--运算练习
    if语句的练习
    switch语句的练习
    九九乘法表
    mac下git提交github代码
  • 原文地址:https://www.cnblogs.com/xiaobai005/p/7908467.html
Copyright © 2011-2022 走看看