I'm assuming here that the goal is to make string building easier,
better, and/or more discoverable, and that the io.StringIO discussion is
just one way to achieve this. For example, I don't think (but maybe I'm
wrong) that "must be a file-like object" is a goal here.
If that's not the goal, then we should decide on the goal first.
On 4/1/2020 5:43 PM, Paul Sokolovsky wrote:
On Wed, 1 Apr 2020 10:01:06 +0100
Paul Moore <p.f.mo...@gmail.com> wrote:
I hope this isn't going to trigger another digression, but it seems to
me that the answer is "nobody, unless they are taught about it, or
work it out for themselves[1]".
Roughly speaking, the answer would be about the same in idea as answers
to the following questions:
* Who'd be using assignment expressions? (2nd way to do assignment,
whoa!)
* Who'd be using f-strings? (3rd (or more) way to do string formatting,
bhoa!)
* Who'd be writing s = s.removeprefix("foo") instead of
"if s.startswith("foo"): s = s[3:]" (PEP616)?
* Who'd be using binary operator @ ?
* Who'd be using using unary operator + ?
I think some of those are bad examples, and not in the same league as
this change. For example, the walrus operator is not just another way to
do assignment (as it existed pre 3.9).
But be that as it may, it seems to me that if you want to make string
building easier, better, and/or more discoverable, then add a
StringBuilder class. It's definitely going to be way more discoverable
than "use io.StringIO" or "look up the FAQ and use ''.join()". And it
could conceivably be more performant than either of those, since it
would have fewer constraints.
You can start with:
class StringBuilder:
def __init__(self):
self.strio = io.StringIO()
def __iadd__(self, s):
self.strio.write(s)
return self
def __str__(self):
return self.strio.getvalue()
And you can later write it in C, make it use ''.join() instead of
io.StringIO, or whatever else makes sense.
I don't think changing the io.StringIO interface is a good decision:
it's supposed to look file-like. I suppose if we could wave a magic wand
and have all existing file-like objects support __iadd__ I might feel
differently, but we can't.
Whether such a StringBuilder is suitable for the stdlib, and if so where
it goes, are questions to be answered if this approach works out.
Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/Y5QFK5JJSFXYH2LG3YQTXLOTQGUW7D3G/
Code of Conduct: http://python.org/psf/codeofconduct/