On Apr 21, 2020, at 01:36, Serhiy Storchaka <storchaka@gmail.com> wrote:
20.04.20 23:33, Andrew Barnert via Python-ideas пише:
Should this print 1 or 2 or raise StopIteration or be a don’t-care? Should it matter if you zip(y, x, strict=True) instead?
It should print 2 in both cases. The only way to determine whether the iterator ends is to try to get its next value. And this value (1) will lost, because there is no way to return it or "unput" to the iterator. There is no reason to consume more values, so StopIteration is irrelevant.
There is more interesting example:
x = iter(range(5)) y = [0] z = iter(range(5)) try: zipped = list(zip(x, y, z, strict=True)) except ValueError: # assuming that’s the exception you want? assert zipped == [(0, 0, 0)] assert next(x) == 2 print(next(z))
Should this print 1 or 2?
The simple implementation using zip_longest() would print 2, but more optimal implementation can print 1.
You’re right; that’s the question I should have asked; thanks. As I said, I think either answer is probably acceptable as long as it’s documented (and, therefore, it’s also clear that the consequences have been thought through).