python特性(十三):context manager的__enter__方法
来源:互联网 发布:网络中文教师招聘 编辑:程序博客网 时间:2024/06/04 18:18
context manager的__enter__和__exit__方法都可以有返回值。__enter__方法的返回值会通过with语句传给调用者,而__exit__方法的返回值是用来控制异常的。本文将介绍__enter__方法。
context manager的一个最重要的应用就是IO操作。这里的IO操作包括文件读写,数据库和网络的连接,进程间通信等。通常一个IO操作不可避免有建立连接,执行操作请求和断开连接三步。我们用下面的一个类来模拟这三个步骤。
class MyFakeConnection: def open(self): print "Connection opened" def request(self): print "A request sent" def close(self): print "Connection closed"
上面的三个步骤中open和close这两个操作是标准的操作,几乎没有任何变化。因此,我们希望能够有一种机制,尽可能大地实现open和close这两个操作的封装。最理想的情况是,我们在主业务逻辑的代码中,完全不需要显式地调用open和close这两个方法。context manager就能帮助我们达到这一目的。请看下面的定义。
class MyContextManager: def __init__(self): self.conn = MyFakeConnection() def __enter__(self): self.conn.open() return self.conn def __exit__(self, *exc): self.conn.close()
我们把open操作放在__enter__方法中,而close操作放在__exit__方法中。注意__enter__方法的返回值是一个MyFakeConnection对象。它是怎么返回给调用者的呢?请看下面的代码。
with MyContextManager() as conn: conn.request()
上面的代码使用as关键字来接收__enter__方法的返回值。代码的输出如下。
Connection openedA request sentConnection closed
阅读全文
0 0
- python特性(十三):context manager的__enter__方法
- Python特性(十四):context manager的__exit__方法
- Python特性(十五): context manager的__exit__方法对异常的处理
- Python特性(十六):由生成器诱导的context manager
- Python with解析 __enter__()、__exit__() (十五)
- Python Study(02)之 Context Manager
- Python Context Manager
- python ------------- __enter__(),__exit__(),with
- Python上下文管理器Context Manager
- python: 上下文管理器(context manager)
- Python学习笔记(五)-- 上下文管理器(Context Manager)
- Python上下文管理器(context manager)一例
- (二十三)JDK8的部分特性
- Python学习8:上下文管理器(context manager)
- 十六、Python 上下文管理 Context Manager
- Python With语句 和context manager
- Python with语句及context manager
- python知识点:上下文管理器[__enter__ 和 __exit__ ]
- Android 反编译
- AngularJs与ReactJS优缺点&适用场景【转】
- angular中json对象push到数组中
- 对抗网络理解(转)
- ubuntu16.04下安装opencv3.2版本
- python特性(十三):context manager的__enter__方法
- 如何修复 Chrome 书签栏图标显示不正常
- leetcode hard模式专杀之23 merge k sorted lists
- ASP.NET简介
- cocos2dx 植物大战僵尸 14 僵尸的攻击
- Kotlin笔记(四)控制流
- C++标准库笔记:13.6 操控器
- Meterial Design 的核心套路layout_behavior的自定义玩法(一)
- 机器学习 F1-Score, recall, precision