重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
器指的是工具,可以定义成函数
创新互联建站是一家专业的成都网站建设公司,我们专注成都网站制作、网站建设、网络营销、企业网站建设,卖链接,广告投放为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。
装饰指的是为其他事务添加额外的东西来点缀
上面两者合到一起:
函数装饰器分为:
开放封闭原则
开放:指的是对扩展功能是开放的
封闭:指的是对修改源代码是封闭的
装饰器就是在不修改被装饰器对象的源代码以及调用方式的前提下,为被装饰对象添加新功能
无参装饰器
没有修改被装饰对象的调用方式,但是改变了源代码
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)
在被装饰对象正上方的单独一行写 @装饰器名字
# 装饰器
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')
即将原函数名指向的内存地址偷梁换柱,所以应该将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)