装饰器

 1 ###装饰器  本质就是函数,修饰其他函数,为其他函数添加附加功能
 2 
 3 ##原则:1.不修改被修饰函数的源代码     2.不修改被修饰函数的调用方式
 4 ##装饰器的知识储备:装饰器==高阶函数  +  函数嵌套  +    闭包
 5 
 6 #高阶函数:变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数
 7 ##1,。函数接受的参数是一个函数名  2 函数的返回值是一个函数名   满足一个条件就行
 8 
 9 ##参数为函数
10 # def foo():
11 #     print("in the foo..")
12 #
13 # def poo(func):
14 #     func()
15 #     print("in the poo..")
16 #
17 # poo(foo)
18 
19 ###返回值为函数
20 # def foo():
21 #     print("in the foo..")
22 # def poo(func):
23 #     print("in the poo..")
24 #     return foo
25 # res=poo(foo)
26 # res()
27 
28 
29 
30 # import time
31 # def cal(l):
32 #     stat_time = time.time()
33 #     res = 0
34 #     for i in l:
35 #         time.sleep(1)
36 #         res +=i
37 #     stop_time = time.time()
38 #     print('函数运行的时间是%s' %(stop_time-stat_time))
39 #     return res
40 #
41 # ll = cal(range(15))
42 # print(ll)
Never compromise.
原文地址:https://www.cnblogs.com/luoluokang/p/12547890.html