一、闭包函数
1、什么是闭包函数?
- 定义在函数内部的函数
- 该函数的函数体代码包含对外部作用域(而不是全局作用域)名字的引用
- 通常将闭包函数用return返回,然后可以在任意位置使用
#爬取页面 import requests #pip3 install requests def outer(url): def get(): respone = requests.get(url) if respone.status_code == 200: print(len(respone.text)) return get baidu=outer(‘https://www.baidu.com‘) python=outer(‘https://www.python.org‘) baidu() python() # 执行结果: 2443 48965
2、闭包函数的意义与应用
1、闭包的意义:返回的函数对象,不仅仅是一个函数对象,在该函数外还包裹了一层作用域,这使得,该函数无论在何处调用,优先使用自己外层包裹的作用域
2、应用领域:延迟计算(原来我们是传参,现在我们是包起来)
二、装饰器
1、什么是装饰器?
装饰器就是闭包函数的一种应用场景。
1、开放封闭原则
对扩展开放,对修改是封闭
2、什么是装饰器
装饰它人的,器指的是任意可调用对象,现在的场景装饰器--》函数,被装饰的对象也是--》函数
装饰器原则:
- 不修改被装饰的源代码
- 不修改被装饰对象的调用方式
装饰器的目的:
在遵循原则1,2的前提下为被装饰对象添加上新功能
import time def index(): time.sleep(3) print(‘welecome to index‘) def timmer(func): def inner(): start=time.time() func() stop=time.time() print(‘run time is %s‘ %(stop-start)) return inner index=timmer(index) index() # 执行结果: welecome to index run time is 3.0019772052764893
三、装饰器修订
装饰器语法:在被装饰对象正上方单独一行写上,@装饰器名
# 旧程序 import time def index(): time.sleep(3) print(‘welecome to index‘) def timmer(func): def inner(): start=time.time() func() stop=time.time() print(‘run time is %s‘ %(stop-start)) return inner index=timmer(index) index() # 改进一:旧程序(index函数)有返回值的情况 import time def timmer(func): def inner(): start=time.time() res=func() stop=time.time() print(‘run time is %s‘ %(stop-start)) return res return inner @timmer #index=timmer(index) def index(): time.sleep(1) print(‘welecome to index‘) return 111 res=index() print(res) # 执行结果: welecome to index run time is 1.0001251697540283 111 # 改进二:旧程序(index函数)有参数的情况 import time def timmer(func): def inner(*args,**kwargs): start=time.time() res=func(*args,**kwargs) stop=time.time() print(‘run time is %s‘ %(stop-start)) return res return inner @timmer #index=timmer(index) def index(name): time.sleep(1) print(‘welecome %s to index‘ %name) return 111 res=index(‘lionel‘) print(res) # 执行结果: welecome lionel to index run time is 1.0033910274505615 111
四、有参装饰器
1、无参装饰器
import time def auth(func): # func=index def inner(*args,**kwargs): name=input(‘name>>: ‘).strip() password=input(‘password>>: ‘).strip() if name == ‘lionel‘ and password == ‘123‘: print(‘login successful‘) return func(*args,**kwargs) else: print(‘login err‘) return inner @auth def index(name): time.sleep(1) print(‘welecome %s to index‘ %name) return 111 res=index(‘lionel‘) # 执行结果: name>>: lionel password>>: 123 login successful welecome lionel to index
2、有参装饰器
import time def auth2(engine=‘file‘): def auth(func): # func=index def inner(*args,**kwargs): if engine == ‘file‘: name=input(‘name>>: ‘).strip() password=input(‘password>>: ‘).strip() if name == ‘lionel‘ 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(‘engine not exists‘) return inner return auth @auth2(engine=‘file‘) def index(name): time.sleep(1) print(‘welecome %s to index‘ %name) return 111 res=index(‘lionel‘) # 执行结果: name>>: lionel password>>: 123 login successful welecome lionel to index
3、并列多个装饰器
import time def timmer(func): def inner(*args,**kwargs): start=time.time() res=func(*args,**kwargs) stop=time.time() print(‘run time is %s‘ %(stop-start)) return res return inner def auth2(engine=‘file‘): def auth(func): # func=index def inner(*args,**kwargs): if engine == ‘file‘: name=input(‘name>>: ‘).strip() password=input(‘password>>: ‘).strip() if name == ‘lionel‘ 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(‘engine not exists‘) return inner return auth @auth2(engine=‘file‘) @timmer def index(name): time.sleep(1) print(‘welecome %s to index‘ %name) return 111 res=index(‘lionel‘) # 执行结果: name>>: lionel password>>: 123 login successful welecome lionel to index run time is 1.001176118850708
五、wraps补充
from functools import wraps import time def timmer(func): @wraps(func) def inner(*args,**kwargs): start=time.time() res=func(*args,**kwargs) stop=time.time() print(‘run time is %s‘ %(stop-start)) return res return inner @timmer def index(name): ‘‘‘index函数。。。。。‘‘‘ time.sleep(1) print(‘welecome %s to index‘ %name) return 111 print(help(index)) # 执行结果: Help on function index in module __main__: index(name) index函数。。。。。 None