4 ms·
For simple context managers, an easier method is to use contextlib.contextmanager (http://docs.python.org/library/contextlib.html http://docs.python.org/library
by ajtulloch 13y ago
For simple context managers, an easier method is to use contextlib.contextmanager (http://docs.python.org/library/contextlib.html http://docs.python.org/library/contextlib.html).
from contextlib import contextmanager
@contextmanager
def tag(name):
print "<%s>" % name
yield
print "</%s>" % name
with tag("h1"):
print "foo"
"""
<h1>
foo
</h1>
"""