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

Victor Stinner victor.stinner at gmail.com
Tue Mar 29 07:15:19 EDT 2016


2016-03-28 17:20 GMT+02:00 Chris Barker - NOAA Federal <chris.barker at noaa.gov>:
>  The OP's
>> 'string'.to_file('some_file') would just be io.write('string',
>> 'some_file'), and str.from_file('some_file') would be
>> io.read('some_file').
>
> Nick's observation about the mental model may be correct ( though I
> don't think so, frankly), but if it is, then this isn't any better
> than:
>
> open("some_path", 'w').write(string)

(Sorry, I didn't follow the whole thread, and I'm not sure that it's
best place for my answer.)

A quick reminder: this pattern raises a ResourceWarning to remind you
that the state of the file is unknown. Flush to disk? Maybe. Maybe
not.

It's a good practice to run Python with -Wd to see this
DeprecationWarning and ResourceWarning. Use -Werror to ensure that you
handle correctly *all* warnings ;-)

Victor


More information about the Python-ideas mailing list