@contextmanager:Python实现with结构的好方法
来源:互联网 发布:linux怎样启动tomcat 编辑:程序博客网 时间:2024/05/18 02:34
常见with结构
常见的with结构是在进行文件操作时,比如
# 中规中矩的写法f = open("test.txt", "w")f.write("hello world!")f.close()# 采用with结构with open("test.txt", "w") as f: f.write("hello world!")
这个结构的好处,一个是简洁,一个是当我们对文件操作的逻辑很长的时候,不会因为忘了关闭文件而造成不必要的错误。类似的,当我们在某些时候不希望遗忘一些重要的语句的时候,可以自己封装个with结构,比如关闭数据库链接等情况。
一般实现方法
with结构一般的实现方法是在定义类的时候重载__enter__
方法和__exit__
方法,比如我们可以通过如下代码来模拟一下上面两段代码前者到后者的转化
# -*- coding: utf-8 -*-class myOpen(): def __init__(self, name, state): self.f = open(name, state) # 返回值是with...as...中as出来的东西,如下面的f def __enter__(self): return self.f # 在with...as...语句块自动执行完之后执行 def __exit__(self, exc_type, exc_val, exc_tb): self.f.close()if __name__ == "__main__": with myOpen("test.txt", "w") as f: f.write("hello world!")
@contextmanager方法
好像上面这种方法实现起来with结构有那么一点点麻烦,那么就进入主题吧,有一种逼格又高,又简便的方法,先上代码,仍以打开文件为例:
# -*- coding: utf-8 -*-from contextlib import contextmanager@contextmanagerdef myOpen(name, state): try: f = open(name, state) yield f finally: f.close()if __name__ == "__main__": with myOpen("test.txt", "w") as f: f.write("hello world!")
可以看出这里只要定义一个函数,然后在它的头部加上@contextmanager就好了,这个函数应该怎么定义呢?我们去源码里看一下就好,里面给出了详细的注释
def contextmanager(func): """@contextmanager decorator. Typical usage: @contextmanager def some_generator(<arguments>): <setup> try: yield <value> finally: <cleanup> This makes this: with some_generator(<arguments>) as <variable>: <body> equivalent to this: <setup> try: <variable> = <value> <body> finally: <cleanup> """ @wraps(func) def helper(*args, **kwds): return GeneratorContextManager(func(*args, **kwds)) return helper
通过注释我们可以看到,我们可以通过给一个try…finally…结构的函数头部加上@contextmanager就可以通过with…as…结构来调用它了,这样try块中yield的数据被as出来,finally块中的数据在with..as..块结束的时候被执行。
这样写出来的代码是不是感觉很nice?
0 0
- @contextmanager:Python实现with结构的好方法
- python with contextmanager
- python装饰器、with和contextmanager
- python学习笔记:利用contextlib和@contextmanager实现with语句上下文实例
- python contextManager详解
- Python标准模块--ContextManager
- Python @contextmanager修饰器
- 通过了装饰器contextmanager实现的上下文管理器,
- 利用宏实现定义结构体的好方法
- python之contextmanager 学习总结
- With语句和上下文管理器ContextManager
- 飘逸的python - 两种with语句实现方法
- 飘逸的python - 两种with语句实现方法
- Python标准模块--ContextManager(上下文管理器)
- python switch结构的实现
- java调用python比较好的方法
- 一个安装python库的好方法
- python 包安装的好方法
- 第八周oj题目求倒数和
- Android系统信息与安全机制
- HDU 1873 看病要排队 (优先队列)
- Intel Edison用mjpg-streamer进行视频传输
- Children of the Candy Corn(图的遍历bfs最小步数)
- @contextmanager:Python实现with结构的好方法
- ajax
- C++中int转化为string
- 一个偷偷写的svm库
- Python version 2.7 required, which was not found in the registry
- Machine Learning --5种距离度量方法
- MySQL5.7.13更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
- jquery eval解析JSON中的注意点介绍
- 洛谷 P1070 道路游戏