[Python-ideas] Add a context manager to keep stream position unchanged

Andrew Barnert abarnert at yahoo.com
Mon Mar 30 11:03:37 CEST 2015


On Mar 30, 2015, at 01:35, Dmitry Kazakov <jsbfox at gmail.com> wrote:
> 
> If the final resolution is "we don't need this in the library", that's fine - I agree that the proposal is trivial and doesn't gain much. OTOH the contextlib.closing can also be said to be a simple exercise, because its implementation is 22 lines long and it's equivalent to
> 
> @contextmanager
> def closing(thing):
>     try:
>         yield thing
>     finally:
>         thing.close()
> 
> (from the docs)
I suspect that if closing were proposed today (or if you were designing Python from scratch today) it never would have made it into contextlib. At the time, nobody had any experience with context managers. And there used to be tons of types that it was useful for; now, most of those types are context managers and therefore don't need closing. Python didn't come with pip, so having things in the stdlib was a lot more important. And it was one of the motivating examples in the PEP, which made it a look like a no-brainer addition.

That doesn't mean there might not be a good argument for this idea. For example, if it turns out to be something that _looks_ trivial to code but is very easy to get subtly wrong, so a lot of people are likely to do it themselves and end up with buggy code, then it sounds like a good candidate. (Well, it would to me, but then I'm not who you have to convince...) But if you agree that it really is trivial, I don't see why it needs to be in the stdlib.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150330/0ed1c080/attachment.html>


More information about the Python-ideas mailing list