目录
装饰器是程序开发中经常遇到的一个功能,用好了装饰器,开发效率如何添翼
一、引出
函数名只是一个变量,只不过指向了一个定义的函数而已,所以才能通过函数名()调用,如果函数名 = xxx被修改了,那么当执行函数名()四,调用的就不是之前的那个函数了
def foo():
print('foo')
foo # 表示的是函数
foo() # 表示的是执行foo函数
def foo():
print('foo')
foo = lambda x:x+1
foo()
#这里执行的是lambda表达式,而不是原来的foo函数,因为foo这个名字被重新指向另外一个匿名函数
现在有个案例
初创公司有N个业务部门,基础平台部门负责提供底层的功能,如:数据库操作、redis调用、监控API等功能。业务部门使用基础功能时,只需调用基础平台提供的功能即可。如下:
############### 基础平台提供的功能如下 ###############
def f1():
print('f1')
def f2():
print('f2')
def f3():
print('f3')
def f4():
print('f4')
############### 业务部门A 调用基础平台提供的功能 ###############
f1()
f2()
f3()
f4()
############### 业务部门B 调用基础平台提供的功能 ###############
f1()
f2()
f3()
f4()
目前公司有条不紊的进行着,但是,以前基础平台的开发人员在写代码时候没有关注验证相关的问题,即:基础平台的提供的功能可以被任何人使用。现在需要对基础平台的所有功能进行重构,为平台提供的所有功能添加验证机制,即:执行功能前,先进行验证。
函数解决方案:
############### 基础平台提供的功能如下 ###############
def check_login():
# 验证1
# 验证2
# 验证3
pass
def f1():
check_login()
print('f1')
def f2():
check_login()
print('f2')
def f3():
check_login()
print('f3')
def f4():
check_login()
print('f4')
写代码要遵循开放封闭
原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即:
- 封闭:已实现的功能代码块
- 开放:对扩展开发
如果将开放封闭原则应用在上述需求中,那么就不允许在函数 f1 、f2、f3、f4的内部进行修改代码
装饰器实现方案:
仅仅对基础平台的代码进行修改,就可以实现在其他人调用函数之前都进行验证操作,并且其他业务部门无需任何操作
def w1(func):
def inner():
# 验证1
# 验证2
# 验证3
func()
return inner
@w1
def f1():
print('f1')
@w1
def f2():
print('f2')
@w1
def f3():
print('f3')
@w1
def f4():
print('f4')
案例解释:
执行w1函数,并将@w1下面的函数作为w1函数的参数,即:@w1等价于w1(f1)
def w1(func):
def inner():
#验证 1
#验证 2
#验证 3
f1() # func是参数,此时 func 等于 f1
return inner
# 返回的 inner代表的是函数,非执行函数
#其实就是将原来的 f1 函数塞进另外一个函数中
@w1
def f1():
print('f1')
也就是说这个函数执行的时候,实现执行w1函数,然后由于w1函数返回的是inner,所以执行inner函数
而inner函数是先执行验证然后再执行f1()函数
所以相当于执行了验证的功能,又执行了原来f1函数的内容,并将原f1函数返回值 返回给业务调用着
二、装饰器简单例子
# 定义函数:完成包裹数据
def makeBold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
# 定义函数:完成包裹数据
def makeItalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makeBold
def test1():
return "hello world-1"
@makeItalic
def test2():
return "hello world-2"
@makeBold
@makeItalic
def test3():
return "hello world-3"
#这里是先把@makeItalic和test3()看成一个整体,然后再执行外面的一步
print(test1())
print(test2())
print(test3())
#输出
<b>hello world-1</b>
<i>hello world-2</i>
<b><i>hello world-3</i></b>
三、装饰器功能
- 引入日志
- 函数执行时间统计
- 执行函数前预备处理
- 执行函数后清理功能
- 权限校验等场景
- 缓存
四、装饰器示例
例1:无参数函数
from time import ctime ,sleep
def timefun(func):
def wrapped_func():
print("%s called at %s" % (func.__name__,ctime()))
func()
return wrapped_func
@timefun
def foo():
print("I am foo!")
foo()
sleep(2)
foo()
#输出
foo called at Wed Jun 24 11:32:57 2020
I am foo!
foo called at Wed Jun 24 11:32:59 2020
I am foo!
例2:被装饰的函数有参数
from time import ctime
def timefun(func):
def wrapped_func(a,b):
print("%s called at %s" % (func.__name__,ctime()))
print("参数是%d,%d "%(a,b))
func(a,b)
return wrapped_func
@timefun
def foo(a,b):
print(a + b)
foo(1,2)
#输出
foo called at Wed Jun 24 11:38:08 2020
参数是1,2
3
例3:被装饰的函数有不定长参数
from time import ctime
def timefun(func):
def wrapped_func(*args,**kwargs):
print("%s called at %s" % (func.__name__,ctime()))
print("参数是",*args,**kwargs)
func(*args,**kwargs)
return wrapped_func
@timefun
def foo(a,b,c,d):
print(a + b + c + d)
foo(1,2,3,4)
#输出
foo called at Wed Jun 24 11:46:49 2020
参数是 1 2 3 4
10
例4:装饰器中的return
from time import ctime
def timefun(func):
def wrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
return func()
return wrapped_func
@timefun
def getInfo():
return '----hahah---'
getInfo()
print(getInfo())
#输出
getInfo called at Wed Jun 24 12:03:55 2020
getInfo called at Wed Jun 24 12:03:55 2020
----hahah---
例5:装饰器带参数,在原有装饰器的基础上,设置外部变量
from time import ctime
def timefun_arg(pre="hello"):
def timefun(func):
def wrapped_func():
print("%s called at %s %s" % (func.__name__, ctime(), pre))
return func()
return wrapped_func
return timefun
@timefun_arg()
def foo():
print("I am foo")
@timefun_arg("python")
def too():
print("I am too")
foo()
too()
#输出
foo called at Wed Jun 24 12:08:24 2020 hello
I am foo
too called at Wed Jun 24 12:08:24 2020 python
I am too
例6:类装饰器(扩展,非重点)
装饰器函数其实是这样一个接口约束,它必须接受一个callable对象作为参数,然后返回一个callable对象。
在python中一般callable对象都是函数,但也有例外。只要某个对象重写了__call__()方法,那么这个对象就是callable的。
class Test():
def __call__(self, *args, **kwargs):
print('call me')
t = Test()
t()
#输出
call me
类装饰器demo
class Test(object):
def __init__(self,func):
print('---初始化---')
print('func name is %s'%func.__name__)
self.__func = func
def __call__(self, *args, **kwargs):
print('---装饰器中的功能---')
self.__func()
@Test
def test():
print('---test---')
test()
#输出
---初始化---
func name is test
---装饰器中的功能---
---test---