
On Mon, Mar 30, 2020 at 10:08:06PM -0700, Guido van Rossum wrote:
On Mon, Mar 30, 2020 at 10:00 PM Steven D'Aprano <steve@pearwood.info> wrote:
it’s optimized for a different use case than string building,
It is? That's odd. The whole purpose of StringIO is to build strings.
I misspoke: it is not the *whole* purpose. See below.
What use-case do you believe it is optimized for?
Let me tell you, since I was there.
StringIO was created in order to fit code designed to a file, where all you want to do is capture its output and process it further, in the same process. (Or vice versa for the reading case of course.) IOW its *primary* feature is that it is a duck type for a file, and that is what it's optimized for. Also note that it only applies to use cases where the data does, indeed, fit in the process's memory somewhat easily -- else you should probably use a temporary file. If the filesystem were fast enough and temporary files were easier to use we wouldn't have needed it.
But it does that by *building a string*, does it not? That's what the getvalue() method is for. Perhaps we're talking past each other. I'm aware that the purpose of StringIO is to offer a file-like API with an in-memory object that doesn't require external storage on the file system. (Hence my retraction above about "whole purpose".) But it still has to do this by returning a string. (In the case of writing to a StringIO object, obviously.) -- Steven