码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习之路:装饰器实现

时间:2017-11-28 11:44:57      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:timer   python学习   highlight   div   nbsp   pre   print   bsp   imp   

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()

 

Python学习之路:装饰器实现

标签:timer   python学习   highlight   div   nbsp   pre   print   bsp   imp   

原文地址:http://www.cnblogs.com/xiaobai005/p/7908467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!