from contextlib import
contextmanager
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
with tag("h1") as r:
print("My string!")
from contextlib import ContextDecorator
class mycontext(ContextDecorator):
def __init__(self, tag):
ContextDecorator.__init__(self)
self.__tag = tag
def __enter__(self):
print("<" + self.__tag + ">")
return self
def __exit__(self, *exc):
print("</" + self.__tag + ">")
return False
with mycontext("h1"):
print("My string!")