您的位置: 网站首页> python进阶> 当前文章

基于生成器实现上下文管理器(contextmanager装饰器)

老董-我爱我家房产SEO2021-11-18196围观,129赞

  上下文管理的实现除了靠定义类实现__enter__和__exit__ 方法之外,也可以基于生成器来实现,不过需要借助python 标准库提供的contextlib模块,contextlib模块提供了contextmanager装饰器来帮我们实现。

  1. from contextlib import contextmanager
  2.  
  3. @contextmanager
  4. def MyFile(filepath,mode):
  5. f = open(filepath,mode=mode,encoding='utf-8')
  6. yield f
  7. print('------')
  8. f.close()
  9.  
  10. with MyFile('1.txt','w') as myf:
  11. myf.write('123\n')
------

  以上代码执行顺序为:

  1、执行MyFile()方法,执行函数内第一行打开文件

  2、执行yield f,MyFile方法返回,f会赋值给with语句块的myf变量

  3、执行with语句块内的逻辑,往文件内写数据

  4、回到MyFile方法中,执行yield后面的代码关闭文件

  在使用contextmanager装饰器时,如果被装饰的方法内发生了异常,则将不会执行yield之后的逻辑,所以我们需要在自己的方法中进行异常处理。

  1. from contextlib import contextmanager
  2.  
  3. @contextmanager
  4. def MyFile(filepath,mode):
  5. f = open(filepath,mode=mode,encoding='utf-8')
  6. yield f
  7. print(haha)
  8. print('------') # 此处代码未执行
  9. f.close()
  10.  
  11. with MyFile('1.txt','w') as myf:
  12. myf.write('123\n')
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\test.py", line 70, in <module>
    myf.write('123\n')
  File "D:\install\python3\lib\contextlib.py", line 119, in __exit__
    next(self.gen)
  File "C:\Users\Administrator\Desktop\test.py", line 65, in MyFile
    print(haha)
NameError: name 'haha' is not defined

  在自己的方法中加入异常处理。

  1. from contextlib import contextmanager
  2.  
  3. @contextmanager
  4. def MyFile(filepath,mode):
  5. try:
  6. f = open(filepath,mode=mode,encoding='utf-8')
  7. yield f
  8. print(haha)
  9. finally:
  10. print('------') # 此处代码未执行
  11. f.close()
  12.  
  13. with MyFile('1.txt','w') as myf:
  14. myf.write('123\n')

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    基于生成器实现上下文管理器(contextmanager装饰器)文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200