装饰函数

python学习网 2017-09-30 12:00:02

Read me:

装饰函数是用来给已有函数增加新的功能的函数,增加新功能的过程中,对已有函数不会做任何修改。常用的方式为:高阶函数+嵌套函数==》装饰函数。在二次开发,升级版本的时候常采用的方式。

def timer(func):
    def deco():
        starttime = time.time()
        func()
        stoptime = time.time()
        print("sumtime: %s" %(stoptime - starttime))
    return deco
@timer
def func1():
    time.sleep(3)


    print("this is func1")
@timer
def func2():
    time.sleep(2)


    print("this is func2")

func1()
func2()
View Code

 

阅读(769) 评论(0)