0

In another answer here that uses contextlib to define a custom "open" function for use with with, contextmanager from contextlib is used to define a function that handles opening and streaming of data and finally closing of the stream.

In learning about this, I see there is also a closing function that seems to work similarly, with a specific focus on closing the stream when done.

I understand how the contextmanager construction presented works (explicitly closing the stream as necessary), but I wonder if it is incomplete - for correctness (and to be Pythonic), should closing be involved as well, or preferred?

Edit: that answer I referred to currently calls fh.close() - I am wondering if somehow closing ought to be involved here in some way instead of that. The documentation on contextlib didn't help me in this either-or-both question in the first place, thus this question.

Community
  • 1
  • 1
MartyMacGyver
  • 9,483
  • 11
  • 47
  • 67

1 Answers1

1

It would be completely inappropriate to stick contextlib.closing around the context manager in that answer, for many reasons:

  1. They don't always want to close the file! That context manager is specifically designed to sometimes leave the file open. This is the entire reason that context manager was written.
  2. When they do want to close the file, the context manager already does that.
  3. Wrapping closing around the context manager would attempt to close the wrong object.

In the case where you do always want to close the file, you usually need neither closing nor a custom context manager, because files are already context managers. Sticking a file in a with statement will close it at the end without needing any special wrappers.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • The answer I referred to doesn't use `closing`, but because it selectively opens a stream it also closes it on exit. I wasn't sure if there was some place for `closing` in that construction, or if `closing` would be an alternative. Doesn't sound like closing has a place here but I don't see how `with` will know to close the appropriate stream on its own either (again, given the way the answer is constructed). Or are you suggesting a `with` inside the contextmanager? Would that even work? – MartyMacGyver Jul 25 '16 at 04:34
  • @MartyMacGyver: Then you should go read about [what `contextlib.closing` does](https://docs.python.org/2/library/contextlib.html#contextlib.contextmanager), and perhaps read [the original PEP for the `with` statement](https://www.python.org/dev/peps/pep-0343/) for some background about how `with` works. `with` "knows" to close the stream because of that `fh.close()` line. – user2357112 Jul 25 '16 at 04:39
  • Ok, I see what you're talking about. I thought you were suggesting fh.close() wasn't really necessary either. I just wondered if `closing` might be somehow preferable to fh.close()... and it sounds like it is definitely not. (It was after reading and re-reading those docs that I ended up here asking my original question... it was a bit vague on which was preferable.) – MartyMacGyver Jul 25 '16 at 04:42