contextmanager
is a decorator. With this decorator, a function is turned into a context manager that can be used in a with
statement. from contextlib import contextmanager @contextmanager def aResource(): print('Acquiring a resource') try: yield finally: print('releasing the resource') with aResource(): print(' do something while the resource is acquired')