17 – Create context managers with contextlib.contextmanager#
The module contextlib provides a decorator contextmanager that you can use to implement your own context managers.
For that, you just create a generator that yields once.
The code before the yield is the setup and the code after the yield is the cleanup.
Whatever you yield (if anything) can be captured by the as ... part of the with statement.
Here is an example that reimplements the built-in open:
from contextlib import contextmanager
@contextmanager
def my_open(path, mode):
try:
file = open(path, mode)
yield file
finally:
file.close()
The trick is that the finally will ensure we close the file, regardless of whether there is an error while working with the open file.