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

Chris Barker - NOAA Federal chris.barker at noaa.gov
Mon Mar 28 11:20:04 EDT 2016


 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)

" I need to open a file to write something to disk"  isn't any harder
to grok than "I need the io module to write something to disk".

It would be nice to not need the 'w' flag, though, but probably way
too dangerous to simply make an file object writable if the write
method is called!

Though I think the way this thread is going is correct - quick reading
and writing to disk requires a serialization format - making the
string is a heavier lift than writing it to disk for anything but very
simple text.

-CHB



>  Granted, both functions are 3 lines of Python,
> but they might well be used enough to be worth adding.
>
> --
> Zach
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list