[Python-ideas] Iterating non-newline-separated files should be easier

Andrew Barnert abarnert at yahoo.com
Fri Jul 18 00:21:25 CEST 2014


> On Thursday, July 17, 2014 2:40 PM, Alexander Heger <python at 2sn.net> wrote:

> >>  I don't think it is reasonable to add a new parameter to readline(), 
> because
>>  streams are widely implemented using duck typing -- every implementation
>>  would have to be updated to support this.
> 
> Could the "split" (or splitline) keyword-only parameter instead be
> passed to the open function (and the __init__ of IOBase and be stored
> there)?


Good idea. It's less powerful/flexible, but probably good enough for almost all use cases. (I can't think of any file where I'd need to split part of it on \0 and the rest on \n…) Also, it means you can stick with the normal __iter__ instead of needing a separate iterlines method.

And, since open/__init__/etc. isn't part of the protocol, it's perfectly fine for the builtin open, etc., to be an example or template that's generally worth following if there's no good reason not to do so, rather than a requirement that must be followed. So, if I'm getting file-like objects handed to me by some third-party library or plugin API or whatever, and I need them to be \0-separated, in many cases the problems with resplit won't be an issue so I can just use it as a workaround, and in the remaining cases, I can request that the library/app/whatever add the sep parameter to the next iteration of the API.

So, I retract my original suggestion in favor of this one. And, separately, Guido's idea of adding the helpers (or at least resplit, plus documentation on how to write the other stuff) to the stdlib somewhere.

Thanks.


More information about the Python-ideas mailing list