
Hello, On Wed, 1 Apr 2020 19:00:00 -0700 Christopher Barker <pythonchb@gmail.com> wrote: []
The funny thing is, in this thread, while I dont really see the need for adding += to StringIO to make a string builder, I kind of like the idea of adding += to the File protocol -- for all file-like objects. I like the compactness of:
with open(filename, 'w') as outfile: a_loop_of_somesort: outfile += something
That's again similar to the feedback received in the 2006 thread: https://mail.python.org/pipermail/python-list/2006-January/396021.html , and just as the original author there, my response is "that's not what I'm talking about". So, why do I think it's good idea to add "+=" to BytesIO/StringIO, but not for other streams (like file objects), are BytesIO/StringIO are somehow special? My answer is yes, they are special. I implied that idea in the original RFC, but didn't want to go in detail with it for the fear of scaring reviewers with far-fetching idea. I don't think there's anything left to lose now, so let me sketch it here (maybe next time, someone will quote not just 2006's thread, but 2020's too). So, stream and buffer protocols are very important, powerful, and deep notion in Python. There's however a question that sometimes it's useful to "shape-shift" from one to another. To have an object which is a cross between a buffer and a stream, or more formally, an "adapter" from one to another. It's my idea that BytesIO/StringIO is the closest what Python has to this buffer/stream "cross-object", and actually, it is already does enough to be *the* cross-object. (Well, BytesIO is *buffer*/stream cross, with StringIO being "natural" extension of that idea, where level of "naturalness" is subject to implementation concerns, like discussion with Andrew Barnert showed). So, just think about it - BytesIO allows to construct data using stream API, and then get that get data as a buffer (using an extension method outside the stream API). Sure, BytesIO can also do other things - you don't have to use .getvalue(). But the fact that BytesIO can do different things is exactly the motivation for proposing to add another operator, +=, that's not going to change the status quo of "does different things" that much. And the operator += being added isn't random - it's buffer's append method, added to BytesIO to make it more of a "cross" between buffer and stream. The last step is just "natural" extension of the construction above to StringIO (with some implementation-level concerns, which definitely can be address as long as the interface part is clear). And surely, this path of making better buffer/stream adapter can be followed further, but not en masse "because we can it", but on a case by case basis. (I for one when stayed clear from that in bounds of this RFC, but made a notice that adding += might definitely be a precedent to drive possible future discussions in that direction.) [] -- Best regards, Paul mailto:pmiscml@gmail.com