[Python-ideas] `to_file()` method for strings
Ethan Furman
ethan at stoneleaf.us
Mon Mar 28 12:38:31 EDT 2016
On 03/28/2016 09:32 AM, Émanuel Barry wrote:
> On Mon, Mar 28, 2016 at 8:54 AM, Émanuel Barry wrote:
>>
>> I wonder how reasonable it would be to add a new keyword to open
>> that would .close() the file object upon a single write/read. Consider:
>>
>> data = open("foo.txt", "r", close_after_use=True).read()
>
> it makes it a tiny bit shorter than using "with", but doesn't solve
> Nick's mental model issue -- the user still needs to be thinking about
> the fact that they are creating a file object and that it needs to be
> closed when you are done with it.
>
> and I"m not sure how you would define "use" -- any i.o. opeartion? i.e.:
>
> infile = open("foo.txt", "r", close_after_use=True)
> first_line = infile.readline()
>
> now what is the state of infile? a closed file object?
Good point. At best this would be an attractive nuisance. Better to
have the `io.read` and `io.write` short cuts.
--
~Ethan~
More information about the Python-ideas
mailing list