装饰器decorator详解及实例
装饰器
一、定义
1.装饰器:本质是函数
2.功能:用来装饰其他函数,为其他函数添加附加功能
二、原则
1.不能修改被装饰函数的源代码
2.不能修改被装饰函数的调用方式
三、实现装饰器
1.函数 即 变量 的概念
2.高阶函数
3.嵌套函数
>> 高阶函数 + 嵌套函数 = 装饰器
四、函数 即 变量
1、函数和变量的类比
x = 1print(id(x))def test():passprint(test)#输出1842348496<function test at 0x0000020DBA24D2F0>
在上例中我们定义了一个变量 "x" 和一个函数 test(),我们分别打印出变量和函数在内存中的位置。可以看出,print(test) 即 print("函数名")的时候,我们可以打印出函数的内存地址。
我们看下面的代码:
def test():print("in the test.")f=testf()#输出in the test.
我们把函数名 test 赋予给 f,然后运行 f(),可以看出函数是可以正常运行的,而且就是test函数的运行结果。那么这和下面的代码是不是类似:
x = 1y = xprint(y)#输出1
我们可以做出如下类比,函数名 test 相当于 x ,函数体就相当于 1,而 f 就相当于 y。
2.python中内存的表现形式
我们把绿方块当作是内存,每一个小方块就是变量或者函数在内存当中的地址。而变量名(x)或者函数名(test),我们可以形象的将他们比作门牌号。当需要调用变量或者函数的时候,我们只要引用他们的门牌号就可以找到他们的内存地址并返回。只是函数的运行需要加(),如 test()。
既然调用变量其实就是引用变量的内存地址,而调用函数名同样可以得到函数体的内存地址。我们就可以把函数名当作变量名传给函数,即函数就是变量。而将函数当作参数的函数,也就是高阶函数。
五、高阶函数
满足下列条件之一就是高阶函数
1.把一个函数名当作实参传给另外一个函数
2.返回值中包含函数名
1)函数名作为参数
import timedef test1():time.sleep(2)print("in the test1.")def test2(func):start_time = time.time()func()stop_time = time.time()print("The action time of program is {}".format(stop_time-start_time))test2(test1)#输出in the test1.The action time of program is 2.0012054443359375
以上事例中,我们定义了一个函数 test1,同时也定义了一个高阶函数test2。我们把test1函数名当作参数传入test2中,可以实现这样一个功能,为原本的test1函数添加了一个计算运行时间的功能。这有点像装饰器了,但是有一点符合,就是上面的高阶函数test2改变了函数的调用方式。
但是我们实现了在不修改被装饰函数的基础上,添加了新功能。
2)返回值中有函数名
def test1():time.sleep(2)print("in the test1.")def test2(func):print(func)return functest1 = test2(test1)test1()#输出<function test1 at 0x000001E5D853D2F0>in the test1.
在上例中,我们最后将高阶函数test2(test1) 赋予给了test1,再次调用test1函数。我们可以直观的看到test1函数的调用方式在此例中没有改变。但是也并没有添加新功能,而这就需要使用到嵌套函数了。
六、嵌套函数
在函数体内又另一个函数的完整定义,这就是嵌套函数。
1)定义:
def foo():print("in the foo")def bar(): print("in the bar")bar()foo()
仅仅在函数内容调用函数,就不是嵌套函数,如下:
def test1(): print("in the test1.")def test2(): test1()
2)嵌套函数的作用域
局部作用域和全局作用域的访问顺序
x = 0def grandpa():x = 1def dad(): x = 2 def son(): x = 3 print(x) son()dad()grandpa()#输出3
3)使用嵌套函数为被修饰函数添加新功能
在高阶函数第二例中,我们实现了不改变原函数的调用方式。而需要添加新功能的话,就要求修饰内容存在在返回值中,即return func 中,我们可以定义一个嵌套函数来实现这个功能。
import timedef timer(func): # timer(test1) func = test1def deco(): start_time = time.time() func() # run test1() stop_time = time.time() print("the action time of the program is {}".format(stop_time-start_time))return deco # 返回了deco的内存地址def test1():time.sleep(2)print("in the test1.")test1 = timer(test1)test1()# 输出 in the test1.the action time of the program is 2.0003786087036133
我们在timer()内部定义了一个嵌套函数 deco(),这个嵌套函数实现了为被修饰函数添加运行时间的功能。而timer()函数返回了deco()的内存地址,这个内存地址deco就可以被引用,甚至直接赋予给 test1。这样我们就可以直接运行 test1(),这样就实现我们的装饰器的功能。
七、装饰器
python通过在函数定义前添加一个装饰器名和@符号,来实现对函数的包装
import timedef timer(func): # timer(test1) func = test1def deco(): start_time = time.time() func() # run test1() stop_time = time.time() print("the action time of the program is {}".format(stop_time-start_time))return deco # 返回了deco的内存地址@timer # test1 = timer(test1)def test1():time.sleep(2)print("in the test1.")test1()