[Python-ideas] `to_file()` method for strings

Chris Barker chris.barker at noaa.gov
Mon Mar 28 12:40:29 EDT 2016


On Mon, Mar 28, 2016 at 9:32 AM, Émanuel Barry <vgr255 at live.ca> wrote:

> 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?
>
> Sure. The keyword really could benefit from a better name, but it might
> solve some part of the issue. It doesn’t solve the mental model issue
> though, but to be fair, open() should always be wrapped in a context
> manager (IMO, anyway).
>

then we don't need anyting else :-)

but the name of the keyword is not that point here -- the issue is what
does it mean to "use" a file object? using "with" let's the user clearly
define, when they are done with the object.

and if a reference to the object is not stored anywhere, then you can be
sure the user doesn't expect to be abel to use it again.

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.

in cPython, the reference counter knows that teh file object has no
references to it when that first line is done running, so it can clean up
and delete the object -- but without a reference counting system, who knows
when it will get cleaned up?

-CHB



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160328/5caa7e66/attachment-0001.html>


More information about the Python-ideas mailing list