Python中當我們們打開文本時,通常會是用with語句,with語句允許我們非常方便的使用資源,而不必擔心資源沒有關閉。
with open('/path/filename', 'r') as f:
f.read()
然而,並不是只有open()函數返回fp對象才能使用 with 語句。實際上,任何對象,只要正確實現上下文管理,就可以使用with語句。實現上下文管理是通過 __enter__ 和 __exit__ 這兩個方法實現的。例如,下面的class實現了這兩個方法:
class Query(object):
def __init__(self, name):
self.name = name
def __enter__(self):
print('Begin')
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type:
print('Error')
else:
print('End')
def query(self):
print('Query info about %s...' % self.name)
這樣我們可以把自己寫的資源對象用於 with 語句。
with Query('Bob') as q:
q.query()
@contextmanager
編寫 __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()
很多時候,我們希望在某段代碼執行前后自動執行特定代碼,也可以用 @contextmanager實現。
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
with tag("h1"):
print("hello")
print("world")
上述代碼執行結果:
<h1> hello world </h1>
代碼的執行順序是:
- with 語句 首先執行 yield 之前的語句,因此打印出 <h1>.
- yield 調用會執行 with 語句內部的所有語句,因此打印出 hello 和 world.
- 最后執行yield之后的語句,打印出 </h1>.
@closing
如果一個對象沒有實現上下文,就不能使用 with 語句,但是可以用 closing() 來把對象變為上下文對象。
from contextlib import closing
from urllib.request import urlopen
with closing(urlopen('https://www.python.org')) as page:
for line in page:
print(line)
closing 也是一個經過 @contextmanager 裝飾的generator
@contextmanager
def closing(thing):
try:
yield thing
finally:
thing.close()
它的作用就是把任意對象變為上下文對象,並支持 with語句。
本文引用於廖雪峰的博客
