标签:foo tran 总结 ror 调用 密码 com 嵌套 文件的
def index(x,y):
print(x,y)
def wrapper(*args,**kwargs):
index(*args,**kwargs) #
# index(y=222,x=111)
wrapper(y=222,x=111)
def index():
return 123
def foo(func):
return func
res = foo(index())
print(res)
输出:123
def outter():
def wrapper():
print(‘Something in wrapper‘)
return wrapper
res = outter()
print(res)
print(res())
输出:
<function outter.<locals>.wrapper at 0x00EB34A8>
Something in wrapper
None
def outter():
x=111
def wrapper():
x
return wrapper
f=outter()
def wrapper(x):
print(1)
print(2)
print(3)
x
wrapper(1)
wrapper(2)
wrapper(3)
def outter(x):
# x=1
def wrapper():
print(1)
print(2)
print(3)
x
return wrapper # return outter内的wrapper那个函数的内地址
# f1=outter(1)
# f2=outter(2)
# f3=outter(3)
wrapper=outter(1)
器指的是工具,可以定义成成函数
装饰指的是为其他事物添加额外的东西点缀
合到一起的解释:装饰器指的定义一个函数,该函数是用来为其他函数添加额外的功能
开放封闭原则
开放:指的是对拓展功能是开放的
封闭:指的是对修改源代码是封闭的
装饰器就是在不修改被装饰器对象源代码以及调用方式的前提下为被装饰对象添加新功能
#需求:在不修改index函数的源代码以及调用方式的前提下为其添加统计运行时间的功能
def index(x,y):
time.sleep(3)
print(‘index %s %s‘ %(x,y))
index(111,222)
# index(y=111,x=222)
# index(111,y=222)
#问题:没有修改被装饰对象的调用方式,但是修改了其源代码
import time
def index(x,y):
start=time.time()
time.sleep(3)
print(‘index %s %s‘ %(x,y))
stop = time.time()
print(stop - start)
index(111,222)
# 问题:没有修改被装饰对象的调用方式,也没有修改了其源代码,并且加上了新功能但是代码冗余
import time
def index(x,y):
time.sleep(3)
print(‘index %s %s‘ %(x,y))
start=time.time()
index(111,222)
stop=time.time()
print(stop - start)
start=time.time()
index(111,222)
stop=time.time()
print(stop - start)
start=time.time()
index(111,222)
stop=time.time()
print(stop - start)
# 问题:解决了方案二代码冗余问题,但带来一个新问题即函数的调用方式改变了
import time
def index(x,y):
time.sleep(3)
print(‘index %s %s‘ %(x,y))
def wrapper():
start=time.time()
index(111,222)
stop=time.time()
print(stop - start)
wrapper()
import time
def index(x,y,z):
time.sleep(3)
print(‘index %s %s %s‘ %(x,y,z))
def wrapper(*args,**kwargs):
start=time.time()
index(*args,**kwargs) # index(3333,z=5555,y=44444)
stop=time.time()
print(stop - start)
# wrapper(3333,4444,5555)
# wrapper(3333,z=5555,y=44444)
import time
def index(x,y,z):
time.sleep(3)
print(‘index %s %s %s‘ %(x,y,z))
def home(name):
time.sleep(2)
print(‘welcome %s to home page‘ %name)
def outter(func):
# func = index的内存地址
def wrapper(*args,**kwargs):
start=time.time()
func(*args,**kwargs) # index的内存地址()
stop=time.time()
print(stop - start)
return wrapper
index=outter(index) # index=wrapper的内存地址
home=outter(home) # home=wrapper的内存地址
home(‘egon‘)
# home(name=‘egon‘)
import time
def index(x,y,z):
time.sleep(3)
print(‘index %s %s %s‘ %(x,y,z))
def home(name):
time.sleep(2)
print(‘welcome %s to home page‘ %name)
def outter(func):
def wrapper(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs)
stop=time.time()
print(stop - start)
return res
return wrapper
# 偷梁换柱:home这个名字指向的wrapper函数的内存地址
home=outter(home)
res=home(‘egon‘) # res=wrapper(‘egon‘)
print(‘返回值--》‘,res)
import time
# 装饰器
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(3)
print(‘index %s %s %s‘ %(x,y,z))
# @timmer # home=timmer(ome)
def home(name):
time.sleep(2)
print(‘welcome %s to home page‘ %name)
index(x=1,y=2,z=3)
home(‘egon‘)
def outter(func):
def wrapper(*args,**kwargs):
# 1、调用原函数
# 2、为其增加新功能
res=func(*args,**kwargs)
return res
return wrapper
def auth(func):
def wrapper(*args,**kwargs):
# 1、调用原函数
# 2、为其增加新功能
name=input(‘your name>>: ‘).strip()
pwd=input(‘your password>>: ‘).strip()
if name == ‘egon‘ and pwd == ‘123‘:
res=func(*args,**kwargs)
return res
else:
print(‘账号密码错误‘)
return wrapper
@auth
def index():
print(‘from index‘)
index()
def outter(func): # func = 函数的内存地址 def wrapper(*args,**kwargs): res=func(*args,**kwargs) return res return wrapper # @outter # index=outter(index) # index=>wrapper @outter # outter(index) def index(x,y): print(x,y) 偷梁换柱之后 index的参数什么样子,wrapper的参数就应该什么样子 index的返回值什么样子,wrapper的返回值就应该什么样子 index的属性什么样子,wrapper的属性就应该什么样子==》from functools import wraps
def auth(func,db_type): def wrapper(*args, **kwargs): name=input(‘your name>>>: ‘).strip() pwd=input(‘your password>>>: ‘).strip() if db_type == ‘file‘: print(‘基于文件的验证‘) if name == ‘egon‘ and pwd == ‘123‘: res = func(*args, **kwargs) return res else: print(‘user or password error‘) elif db_type == ‘mysql‘: print(‘基于mysql的验证‘) elif db_type == ‘ldap‘: print(‘基于ldap的验证‘) else: print(‘不支持该db_type‘) return wrapper # @auth # 账号密码的来源是文件 def index(x,y): print(‘index->>%s:%s‘ %(x,y)) # @auth # 账号密码的来源是数据库 def home(name): print(‘home->>%s‘ %name) # @auth # 账号密码的来源是ldap def transfer(): print(‘transfer‘) index=auth(index,‘file‘) home=auth(home,‘mysql‘) transfer=auth(transfer,‘ldap‘) # index(1,2) # home(‘egon‘) # transfer()
def deco(func): def wrapper(*args, **kwargs): name=input(‘your name>>>: ‘).strip() pwd=input(‘your password>>>: ‘).strip() if db_type == ‘file‘: print(‘基于文件的验证‘) if name == ‘egon‘ and pwd == ‘123‘: res = func(*args, **kwargs) return res else: print(‘user or password error‘) elif db_type == ‘mysql‘: print(‘基于mysql的验证‘) elif db_type == ‘ldap‘: print(‘基于ldap的验证‘) else: print(‘不支持该db_type‘) return wrapper return deco deco=auth(db_type=‘file‘) @deco # 账号密码的来源是文件 def index(x,y): print(‘index->>%s:%s‘ %(x,y)) deco=auth(db_type=‘mysql‘) @deco # 账号密码的来源是数据库 def home(name): print(‘home->>%s‘ %name) deco=auth(db_type=‘ldap‘) @deco # 账号密码的来源是ldap def transfer(): print(‘transfer‘) index(1,2) home(‘egon‘) transfer()
def auth(db_type): def deco(func): def wrapper(*args, **kwargs): name = input(‘your name>>>: ‘).strip() pwd = input(‘your password>>>: ‘).strip() if db_type == ‘file‘: print(‘基于文件的验证‘) if name == ‘egon‘ and pwd == ‘123‘: res = func(*args, **kwargs) # index(1,2) return res else: print(‘user or password error‘) elif db_type == ‘mysql‘: print(‘基于mysql的验证‘) elif db_type == ‘ldap‘: print(‘基于ldap的验证‘) else: print(‘不支持该db_type‘) return wrapper return deco @auth(db_type=‘file‘) # @deco # index=deco(index) # index=wrapper def index(x, y): print(‘index->>%s:%s‘ % (x, y)) @auth(db_type=‘mysql‘) # @deco # home=deco(home) # home=wrapper def home(name): print(‘home->>%s‘ % name) @auth(db_type=‘ldap‘) # 账号密码的来源是ldap def transfer(): print(‘transfer‘) # index(1, 2) # home(‘egon‘) # transfer()
def 有参装饰器(x,y,z): def outter(func): def wrapper(*args, **kwargs): res = func(*args, **kwargs) return res return wrapper return outter @有参装饰器(1,y=2,z=3) def 被装饰对象(): pass
def deco1(func1): #func1 = wrapper2的内存地址 def wrapper(*args,**kwargs): print(‘正在运行======>deco1.wrapper1‘) res1 = func1(*args,**kwargs) # func1是wrapper2 return res1 return wrapper def deco2(func2): #func2 = wrapper3的内存地址 def wrapper2(*args,**kwargs): print(‘正在运行======>deco2.wrapper2‘) res2 = func2(*args,**kwargs) # func2是wrapper3 return res2 return wrapper2 def deco3(x): def outter3(func3): #func3 = 被装饰对象index的内存地址 def wrapper3(*args,**kwargs): print(‘正在运行======>deco2.wrapper3‘) res3 = func3(*args,**kwargs) # func3是index return res3 return wrapper3 return outter3 # 加载顺序:自下而上(了解) @deco1 # deco1(wrapper2的内存地址) ===> index = wrapper1的内存地址 @deco2 # deco2(wrapper3的内存地址) ===> index = wrapper2的内存地址 @deco3(111) # ===> @outter3 ===> index = outter3(index) ===> index = wrapper3的内存地址 def index(x,y): print(‘from index %s:%s‘%(x,y)) index(1,2) # 执行顺序:自上而下 即:wrapper1 ==> wrapper2 ==> wrapper3 # index(1,2) # wrapper1(1,2)
标签:foo tran 总结 ror 调用 密码 com 嵌套 文件的
原文地址:https://www.cnblogs.com/zhww/p/12983920.html