LoginSignup
0
1

More than 3 years have passed since last update.

contextlib.contextmanager

Posted at
import contextlib


# def tag(name):
#     def _tag(f):
#         def _wrapper(content):
#             print('<{}>'.format(name))
#             r = f(content)
#             print('</{}>'.format(name))
#             return r
#         return _wrapper
#     return _tag
#
#
# @tag('h2')
# def f(content):
#     print(content)
#
# #f = tag('h2')(f)
#
# f('test')

@contextlib.contextmanager
def tag(name):
    print('<{}>'.format(name))
    yield
    print('</{}>'.format(name))


# @tag('h2')
# def f(content):
#     print(content)
#
# f('test')

def f():
    print('test0')
    with tag('h2'):
        print('test')
        with tag('h5'):
            print('test2')


f()

実行結果:

test0
<h2>
test
<h5>
test2
</h5>
</h2>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1