[Python-ideas] `to_file()` method for strings
Alexander Belopolsky
alexander.belopolsky at gmail.com
Mon Mar 28 13:24:54 EDT 2016
On Mon, Mar 28, 2016 at 12:40 PM, Chris Barker <chris.barker at noaa.gov>
wrote:
> but:
>
> data = open("foo.txt", "r", close_after_use=True).read()
>
> and
>
> infile = open("foo.txt", "r", close_after_use=True)
> data = infile.read()
>
> look exactly the same to the file object itself, it has no idea when the
> user is done with it.
It is my understanding that under the "close_after_use" proposal, infile
will be closed by .read() in both cases.
Still, I don't like this idea. I puts action specification (close) too far
from where the action is taken. The two-line example already makes me
uncomfortable and imagine if infile is passed through several layers of
function calls before infile.read() is called.
I would rather see read_and_close() and write_and_close() convenience
methods for file objects:
data = open("foo.txt").read_and_close()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160328/e7bb7301/attachment.html>
More information about the Python-ideas
mailing list