20180419-装饰器2

1.    嵌套函数:在函数中定义一个新的函数

2.    *args,**kwargs:传递参数

import time

def timer(func):
    def deco(*args,**kwargs):
        start_time = time.time()
        func(*args,**kwargs)
        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")

#test1()
@timer
def test2(name,age):
    print("test1",name,age)

test2("li",22)


1,timer只是把定义的函数deco内存地址重新赋值给函数名test2
2,定义的deco函数重新添加了功能,并且可以传入参数,*args,**kwargs

3,在deco函数中调用了原来的函数test2功能

猜你喜欢

转载自blog.csdn.net/weixin_42020378/article/details/80011735