4 ms·
One minor downside to `with` is how it brings normal control flow out to the right.
by mappu 9y ago
One minor downside to `with` is how it brings normal control flow out to the right.
- eesmith 9y agoIf that's an issue, one option might be https://docs.python.org/3/library/contextlib.html?#contextlib.ExitStack https://docs.python.org/3/library/contextlib.html?#contextli... .
- make3 9y agovery nice
- Animats 9y agoThat's really complicated. The great thing about the "with" clause is that it Just Works. The easiest way to handle opens and closes is thus exception-safe. What's wrong with indenting? You're entering a scope; indenting is appropriate.
- d4l3k 9y agoWhat happens when you have to enter "with" something an arbitrary number of times? That gets very painful.
- eesmith 9y agoI wrote "if that's an issue". For most cases it's not an issue. The primary use case ExitStack is not to reduce indentation levels; it's to support "a variable number of context managers and other cleanup operations in a single with statement". You wrote "The easiest way to handle opens and closes is thus exception-safe". Consider if you need to merge up to 50 files containing sorted lines, where the result must also be sorted, and where you should not load all of the data into memory first. (Sorted according to UTF-8.) How would you do it? Not so easy, is it? With ExitStack you could write it as something like the following sketch: filenames = [ list, of, file, names] with ExitStack() as stack: files = [stack.enter_context(open(fname, "rb")) for fname in filenames] with open("merged_lines.txt", "wb") as output: output.writelines(heapq.merge(*files)) As you say, the great thing about this with clause is that it Just Works. A side-effect of making ExitStack work is that it might help if the indentation depth is a problem, which mappu apparently has experienced. As another example of where Python registers a cleanup rather than use a context manager, see https://docs.python.org/3/library/unittest.html#unittest.TestCase.addCleanup https://docs.python.org/3/library/unittest.html#unittest.Tes... .