[Python-ideas] `to_file()` method for strings
Andrew Barnert
abarnert at yahoo.com
Thu Mar 24 16:36:17 EDT 2016
On Mar 24, 2016, at 11:02, Stephen J. Turnbull <stephen at xemacs.org> wrote:
>
> Andrew Barnert via Python-ideas writes:
>
>> I do it all the time in other languages when dealing with smallish
>> files. Python's very nice file-object concept, slant toward
>> iterator-based processing, and amazingly consistent ecosystem means
>> that the same issues don't apply, so I'd rarely do the same
>> thing. But for users migrating to Python from another language, or
>> using Python occasionally while primarily using another language, I
>> can see it being a lot more attractive.
>
> It occurs to me that we already have a perfectly appropriate builtin
> for the purpose anyway: print. Add a filename= keyword argument, and
> make use of both file= and filename= in the same call an error. If
> that's not the right answer, I don't see how str.to_file() can
> possibly be better.
That's not a bad idea.
However, it's obvious how to extend str.to_file() to reads (str.from_file() classmethod), binary data (bytes.to_file()), atomic writes (str.to_file(path, atomic=True), non-default encodings (str.to_file(path, encoding='Latin-1', errors='replace')). With print, those are all less obvious. Maybe input() for reads, but how can it handle bytes? And, while you could toss on more parameters to print() for some of these, it already has a lot, and adding new parameters that are only legal if filename is specified seems like it'll be make the docs even harder to follow.
Of course if the answers are "we don't want any of those", then being more limited is a good thing, not a problem. :)
More information about the Python-ideas
mailing list