装饰器
(1)什么是装饰器:
-
器指的是工具,可以定义成函数
-
装饰指的是为其他事务添加额外的东西来点缀
上面两者合到一起:
- 装饰器指的是定义一个函数,该函数用来为其他函数添加额外的功能
函数装饰器分为:
- 无参装饰器和有参装饰两种,二者的实现原理一样,都是’函数嵌套+闭包+函数对象’的组合使用的产物。
(2)为何要用装饰器
开放封闭原则
开放:指的是对扩展功能是开放的
封闭:指的是对修改源代码是封闭的
装饰器就是在不修改被装饰器对象的源代码以及调用方式的前提下,为被装饰对象添加新功能
(3)装饰器实现思路
无参装饰器
- 方案一:失败
没有修改被装饰对象的调用方式,但是改变了源代码
def index(x, y):
start = time.time()
time.sleep(2)
print(' index %s %s ' % (x, y))
end = time.time()
print(end - start)
index(1, 2)
# index()
- 方案二
# 问题:没有修改被装修饰对象的源代码,也灭有修改调用方式,但是代码冗余
def index(x, y):
time.sleep(2)
print(' index %s %s ' % (x, y))
start = time.time()
index(11, 22)
end = time.time()
start = time.time()
index(11, 22)
end = time.time()
- 方案三
问题:解决了代码荣誉,但是函数的调用方式改变了
def index(x, y):
time.sleep(2)
print(' index %s %s ' % (x, y))
def wrapper():
start = time.time()
index(11, 22)
end = time.time()
wrapper()
wrapper()
wrapper()
- 方案三的优化一:
在方案三基础上优化代码:将Index写活了(参数写活了)
def index(x, y,z):
time.sleep(2)
print(' index %s %s %s ' % (x, y,z))
def wrapper(*args,**kwargs):
start=time.time()
index(*args,**kwargs)
stop=time.time()
print(stop-start)
wrapper(11,22,44)
wrapper(111,y=111,z=4545)
-
方案三的优化二:
-
在优化一的基础上把被装饰对象写活,原来只能装饰Index
def index(x, y, z):
time.sleep(2)
print(' index %s %s %s ' % (x, y, z))
def home(name):
time.time()
print('welcome %s to home page' % name)
def outer(func): # func=index的内存地址
# func = index
def wrapper(*args, **kwargs):
start = time.time()
func(*args, **kwargs) # index的内存地址()
stop = time.time()
print(stop - start)
return wrapper
home=outer(home)
home('zhao')
# f = outer(index) # f=outer(index的内存地址)
# f(x=1, y=2, z=3)
index = outer(index) # 偷梁换柱.>>此时的index指向的是wrapper的内存地址
print(index)
index(x=1, y=2, z=3)
-
方案三的优化三
将wrapper做的跟被装饰器一摸一样,以假乱真
def index(x, y, z):
time.sleep(2)
print(' index %s %s %s ' % (x, y, z))
def home(name):
time.time()
print('welcome %s to home page' % name)
return 1234
def outer(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
stop = time.time()
print(stop - start)
return res
return wrapper
home = outer(home)
res = home('zhao')
print('返回值:>>>', res)
(4) 语法糖:
在被装饰对象正上方的单独一行写 @装饰器名字
# 装饰器
def timmer(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
stop = time.time()
print(stop - start)
return res
return wrapper
@timmer # index=timmer(index)
def index(x, y, z):
time.sleep(2)
print(' index %s %s %s ' % (x, y, z))
@timmer # home = timmer(home)
def home(name):
time.sleep(2)
print('welcome %s to home page' % name)
return 1234
index(x=1,y=2,z=3)
home('zhao')
(5)偷梁换柱
即将原函数名指向的内存地址偷梁换柱,所以应该将wrapper做的跟原函数一样才行
手动的将原函数的属性值赋值给wrapper,需要一个一个的去加,太麻烦
def outter(func):
def wrapper(*args, **kwargs):
#手动的将原函数的属性值赋值给wrapper
# 函数名wrapper.__name__ =原函数.__name__
# 函数名wrapper.__doc__ = 原函数.__doc__
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
res = func(*args, **kwargs)
return res
return wrapper
@outter # index=outter(index)
def index(x, y):
"""
:param x:
:param y:
:return:
"""
print('index:', x, y)
index(1, 2)
print(index.__name__)
print(index.__doc__) # help(index)
自动的将原函数的所有属性值赋值给wrapper
from functools import wraps
def outter(func):
@wraps(func)
def wrapper(*args, **kwargs):
#手动的将原函数的属性值赋值给wrapper
# 函数名wrapper.__name__ =原函数.__name__
# 函数名wrapper.__doc__ = 原函数.__doc__
# wrapper.__name__ = func.__name__
# wrapper.__doc__ = func.__doc__
res = func(*args, **kwargs)
return res
return wrapper
@outter # index=outter(index)
def index(x, y):
"""
:param x:
:param y:
:return:
"""
print('index:', x, y)
index(1, 2)
print(index.__name__)
print(index.__doc__) # help(index)
- 无参装饰器模板
def outter(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
return wrapper()
- 统计时间的装饰器
def timmer(func):
def wrapper(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs)
end=time.time()
print(end-start)
return res
return wrapper()
- 认证功能
def auth(func):
def wrapper(*args, **kwargs):
name = input('your name :').strip()
passwd = input('your password:').strip()
if name == 'zhao' and passwd == '132':
res = func(*args, **kwargs)
return res
else:
print("your name or your password is error")
return wrapper
@auth
def index():
print('from index')
index()
- 有参装饰器模板
def 有参装饰器(x,y,z)
def outter(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
return wrapper()
@有参装饰器(1,y=2,z=3)
def 被装饰对象():
pass
- 认证功能改进
def auth(db_type):
def deco(func):
def wrapper(*args, **kwargs):
username = input('your name:').strip()
password = input('your paddword').strip()
if db_type == 'file':
print('基于文件验证')
if username == 'zhao' and password == '133':
print('login successful')
res = func(*args, **kwargs)
return res
else:
print('username or password is error')
elif db_type == 'mysql':
print("基于数据库")
elif db_type == 'ldap':
print("基于ldap")
else:
print('不支持该db_type')
return wrapper
return deco
@auth(db_type='file')#@deco #index=dexo(index)
def index(x, y):
print('index:>>%s %s' % (x, y))
@auth(db_type='mysql')
def home(name):
print('home :>>%s' % name)
@auth(db_type='ldap')
def transfer():
print("transfer:>>>%s" % transfer)
index(1, 2)
home('zhao')
transfer()
- 叠加多个装饰器分析
def deco1(func1): # func1=wrapper2的内存地址
def wrapper1(*args, **kwargs):
print('deco1.wrapper1')
res1 = func1(*args, **kwargs)
return res1
return wrapper1
def deco2(func2): # func2=wrapper3的内存地址
def wrapper2(*args, **kwargs):
print('deco1.wrapper2')
res2 = func2(*args, **kwargs)
return res2
return wrapper2
def deco3(x):
def outter(func3): # func3=被装饰对象index函数的内存地址
def wrapper3(*args, **kwargs):
print('deco3.outter.wrapper3')
res3 = func3(*args, **kwargs)
return res3
return wrapper3
return outter
# 加载顺序:自下而上
@deco1 # index=deco1(wrapper2的内存地址) ===》index=wrapper1的内存地址
@deco2 # index=deco2(wrapper3的内存地址)===》index=wrapper2的内存地址
@deco3(11) # @outer===>@index=outer(index)===>index=wrapper3的内存地址
def index(x, y):
print('from index %s %s' % (x, y))
print(index)
# 执行顺序:自上而下即 wrapper1>wrapper2>wrapper3
#
index(1, 2) # wrapper1(1,2)
标签:index,return,python,wrapper,time,print,装饰,def
From: https://www.cnblogs.com/saury/p/16739191.html