标签:important from 管理 initial class ble 作用 position sans
contextlib是一个Python模块,作用是提供更易用的上下文管理器。
编写 __enter__ 和 __exit__ 仍然很繁琐,因此Python的标准库 contextlib 提供了更简单的写法,
比如如下代码:
from contextlib import contextmanager class Query(object): def __init__(self, name): self.name = name def query(self): print(‘Query info about %s...‘ % self.name) @contextmanager def create_query(name): print(‘Begin‘) q = Query(name) yield q print(‘End‘)
@contextmanager 这个装饰器接受一个 generator,用 yield 语句把 with ... as var 把变量输出出去,然后,with 语句就可以正常的工作了:
with create_query(‘Bob‘) as q: q.query()代码的执行顺序是:
如果一个对象没有实现上下文,就不能使用 with 语句,但是可以用 closing() 来把对象变为上下文对象。
|
1
2
3
4
5
6
|
from contextlib import closingfrom urllib.request import urlopenwith closing(urlopen(‘https://www.python.org‘)) as page: for line in page: print(line) |
closing 也是一个经过 @contextmanager 装饰的generator
|
1
2
3
4
5
6
|
@contextmanagerdef closing(thing): try: yield thing finally: thing.close() |
它的作用就是把任意对象变为上下文对象,并支持 with语句。
@contextmanager 这个装饰器接受一个 generator,用 yield 语句把 with ... as var 把变量输出出去,然后,with 语句就可以正常的工作了:
|
1
2
|
with create_query(‘Bob‘) as q: q.query() |
标签:important from 管理 initial class ble 作用 position sans
原文地址:http://www.cnblogs.com/aomi/p/7353939.html