On Mon, Apr 27, 2020 at 4:39 PM Christopher Barker <pythonchb@gmail.com> wrote:
Isn't there? There are many cases where you CANNOT (or don't want to, for performance reasons) "consume" the entirely of the inolut iterators, and many cases where it would be fine to do that. But are there many (any?) cases where you couldn't use the "sentinal approach".
It depends what you mean by "cannot." Algorithmically of course you can use sentinel. But the issue is computation cost and rollback of side-effects. E.g. change my example code just slightly: for p, q, m in zip_longest(bigints1, bigints2, bigints3, fillvalue=_sentinel): if _sentinel in pair: raise UnequalLengthError("uh oh") result = p**q % m store_to_db(result) If we have 1000-digit numbers, but only a couple thousand of them, we would be vastly better off checking the lengths in advance (if that is possible, and if generating the numbers in the first place is comparatively cheap).
Sure: but that is a distinction that is, as far as I know, never made in the standard library with all the "iterator related" code. There are some things that require proper sequences, but as far as I know, nothing that expects a "concretizable" iterator -- and frankly, I'm don't think there is a clear definition of that anyway
Oh... absolutely. "Concretizable" is very task-specific. Other than infinite iterators, any iterator could be wrapped in list() to *eventually* get a concrete sequence. This isn't a Python language distinction but a "what do you want to do?" distinction. If there were a zip_equal() in itertools, would you ever write the code to
use zip_longest and check the sentinel? For my part, I wouldn't, and indeed once I had a second need for it, I'd write zip_equal for my own toolbox anyway :-)
I dunno. I guess I might use zip_equal() in the case where I didn't want to bother with an `except` and just let the program crash on mismatch (or maybe catch with a generic "something went wrong" kind of status). Whenever I really want specific remediation action, I think I'd still prefer the sentinel. Often enough I still can do *something* with the non-exhausted elements from the other iterators that it feels like a more general pattern. FWIW, if it is added, I like the name zip_strict() better than zip_equal(). But someone else is building the bikeshed, so I'm not that worried about spelling. -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.