任何對象谴返,只要正確實現了上下文管理,就可以用于with語句。實現上下文管理是通過__enter__
和__exit__
這兩個方法實現的。
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 Query('Bob') as q:
q.query()
@contextmanager
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')
with create_query('Bob') as q:
q.query()
很多時候同欠,我們希望在某段代碼執(zhí)行前后自動執(zhí)行特定代碼,也可以用@contextmanager
實現横缔。例如:
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
with tag("h1"):
print("hello")
print("world")
# 測試
<h1>
hello
world
</h1>
@closing
如果一個對象沒有實現上下文铺遂,我們就不能把它用于with
語句。這個時候茎刚,可以用closing()
來把該對象變?yōu)樯舷挛膶ο蠼笕瘛@纾?code>with語句使用urlopen()
:
from contextlib import closing
from urllib.request import urlopen
with closing(urlopen('https://www.python.org')) as page:
for line in page:
print(line)