python :装饰器

装饰器:本质是一个函数,就是为其他函数添加附件功能
特性:1.不能修改被装饰函数的源代码
2.不能修改被装饰函数的调用方式
#装饰器
import time
def deco(func):
    def gao(*args,**kwargs):
        start_time=time.time()
        func(*args,**kwargs)
        stop_time=time.time()
        print("in the func %s" %(stop_time-start_time))
    return gao
@deco
def test():
    time.sleep(1)
    print("in the test")
def test2(name): #test2=deco(test2)=gao
    time.sleep(1)
    print("in the test2",name)
test()
test2("alex")

 

posted @ 2016-10-13 22:39  梅子472  阅读(116)  评论(0编辑  收藏  举报