Is it safe to combine ‘with’ and ‘yield’ in python?
You bring up a criticism that has been raised before1. The cleanup in this case is non-deterministic, but it will happen with CPython when the generator gets garbage collected. Your mileage may vary for other python implementations… Here’s a quick example: from __future__ import print_function import contextlib @contextlib.contextmanager def manager(): “””Easiest way to get a … Read more