一、上下文管理器

  with是python实现上下文管理器的核心关键词。它能够在代码执行前和执行后做一些额外的事情。

  最常见的代码恐怕就是文件操作了。

with open("file", "r", encoding="utf-8") as f:
data = f.read()
import functools
reader = functools.partial(open, mode="r", encoding="utf-8")
f = reader("homework/user.txt")
f.readlines()

  实际上,with语句是通过__enter__和__exit__来实现的。

class Open(object):
def __init__(self, file, mode, encoding="GBK"):
"""初始化,生成文件句柄"""
self.file = file
self.mode = mode
self.encoding = encoding def __enter__(self):
print("__enter__ has called.")
try:
f = open(self.file, self.mode, self.encoding)
return f
except Exception as e:
print(e) def __exit__(self, exc_type, exc_val, exc_tb):
print("__exit__ has called.")
print("exc_type: ", exc_type)
print("exc_val: ", exc_val)
print("exc_tb: ", exc_tb) with Open("有关协议.docx", mode='r', encoding="GBK") as f:
data = f.readlines() """
__enter__ has called.
an integer is required (got type str) # 异常提示信息
__exit__ has called.
exc_type: <class 'AttributeError'>
exc_val: 'NoneType' object has no attribute 'readlines'
exc_tb: <traceback object at 0x110ade608>
"""

  with在开始时调用__enter__,并将结果赋给as后面的变量f。在结束时会调用__exit__,如果有异常,则会把异常类型、异常值和跟踪地址作为参数传递给__eixt__。

  如果我们自己使用with,则必须在定义的类中实现__enter__和__exit__方法。

二、contextlib模块

  python3内置了contextlib模块来帮助我们更加方便地实现上下文管理器。

import contextlib
@contextlib.contextmanager
def file_open(file_name, mode="r", encoding="utf-8"):
print("file open")
yield open(file_name, mode=mode, encoding=encoding)
print("file end") with file_open("log.txt") as f:
print("Read file.")
print(f)
dic = f.read()
print(dic)

  contextlib必须调用装饰器来装饰一个需要使用with语句的函数。在函数内部必须要使用yield将该函数变成可迭代对象。

  在with时,将yield返回值赋给as后的变量。此时已执行到yield时。f包裹的内容作为do something继续执行。最后才会执行file end。

最新文章

  1. hustoj1353 节点选择 树形dp
  2. css flexbox水平垂直
  3. js程序设计02——变量、作用域问题
  4. java错题本
  5. AE 栅格处理
  6. 在Eclipse中用TODO标签管理任务
  7. [cocos2d-x&#183;解Bug]关于cocos2d-x游戏在android锁屏状态下播放Bgm的解决方法
  8. 深入浅出Zookeeper
  9. Ajax请求用户控件(.ascx)404错误
  10. cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
  11. 快速搞定用Vue+Webpack搭建前端项目(学习好久了,该写点东西了......)
  12. 快速拥有各种数据访问SqlHelper
  13. Java线程和守护进程
  14. ListIterator
  15. Kafka学习笔记2: 快速入门
  16. 【Python3爬虫】微博用户爬虫
  17. Java里面使用Date.compareTo比较时间
  18. Day13 Python基础之time/datetime/random模块一(十一)
  19. Leetcode 记录(101~200)
  20. oracle中常用的对用户的操作

热门文章

  1. [ActionScript 3.0] SharedObject的用法简介
  2. 算法 PK 猫咪 | 章鱼保罗后继竟然是只猫?
  3. python 数据分析 文章集锦
  4. Flink学习笔记:Flink开发环境搭建
  5. 快速上手日期插件daterangepicker
  6. 检查java 中有多少个构造函数
  7. document.documentElement和document.body区别介绍
  8. Maven 远程仓库下载慢的的解决方案
  9. java数据结构和算法学习笔记
  10. 在c#中使用sqlite3