网络平台建设费用,windows优化大师兑换码,wordpress 高亮,沈阳微营销网站制作装饰器 #1、开放封闭原则#xff1a;对扩展开放#xff0c;对修改是封闭#2、装饰器#xff1a;装饰它人的#xff0c;器指的是任意可调用对象#xff0c;现在的场景装饰器-》函数#xff0c;被装饰的对象也是-》函数#原则#xff1a;1、不修改被装饰对象的源代码 2、不修…装饰器 #1、开放封闭原则对扩展开放对修改是封闭#2、装饰器装饰它人的器指的是任意可调用对象现在的场景装饰器-》函数被装饰的对象也是-》函数#原则1、不修改被装饰对象的源代码 2、不修改被装饰对象的调用方式#装饰器的目的在遵循1,2的前提下为被装饰对象添加上新功能 1无参数类型 import time
def outer(func):def inner():time.sleep(1)print(hello)func()return innerdef bar():print(world) 2有参数类型 # 有参装饰器
import timedef auth2(enginefile):def auth(func): # funcindexdef inner(*args,**kwargs):if engine file:nameinput(name: ).strip()passwordinput(password: ).strip()if name egon and password 123:print(login successful)return func(*args,**kwargs)else:print(login err)elif engine mysql:print(mysql auth)elif engine ldap:print(ldap auth)else:print(engin not exists)return innerreturn authauth2(enginemysql) #auth #indexauth(index) #indexinner
def index(name):time.sleep(1)print(welecome %s to index %name)return 1111resindex(egon) #resinner(egon)
print(res) 3并列装饰器 import time
def timmer(func):def inner(*args,**kwargs):starttime.time()resfunc(*args,**kwargs)stoptime.time()print(run time is %s %(stop-start))return resreturn innerdef auth2(enginefile):def auth(func): # funcindexdef inner(*args,**kwargs): # 一致if engine file:nameinput(name: ).strip()passwordinput(password: ).strip()if name egon and password 123:print(login successful)res func(*args,**kwargs) #一致return reselse:print(login err)elif engine mysql:print(mysql auth)elif engine ldap:print(ldap auth)else:print(engin not exists)return innerreturn authauth2(enginefile)
timmer
def index(name):time.sleep(1)print(welecome %s to index %name)return 1111resindex(egon)
print(res) 4 from functools import wraps
import time
def timmer(func):wraps(func)def inner(*args,**kwargs):starttime.time()resfunc(*args,**kwargs)stoptime.time()print(run time is %s %(stop-start))return res# inner.__doc__func.__doc__# inner.__name__func.__name__return innertimmer
def index(name): #indexinnerindex 函数。。。。。time.sleep(1)print(welecome %s to index %name)return 1111resindex(egon)
print(res)print(help(index)) 转载于:https://www.cnblogs.com/taozizainali/p/8202361.html