Dubslow <buns...@gmail.com> added the comment:

Regarding the current bug more specifically, I think a few comments would go a 
long way to helping readers understand the code.

And also, I do think the (1, +1, -1) is less readable, simply because it 
doesn't follow the most common usage patterns of range, where your first 
version using (0,0,0) (with implicit zeroes of course) is cleaner. It's much 
more apparent how long the loop is at first glance ("loop once per iter" vs 
"loop... from what to what? oh, once per iter"). Perhaps not using reversed() 
but instead setting step=-1 would be a better case to use the off-by-1 variant.

Such a version with both proposed changes (which are independent and can be 
considered or accepted separately) looks like this:


def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # This recipe is also called other names like "alternate", "interleave", or
    # "merge". "zip_flat" would also be an accurate name.
    # Algorithm: cycle around the __next__ methods of each iterable. when an
    # iter runs out, remove its __next__ from the cycle.
    nexts = cycle(iter(it).__next__ for it in iterables)
    for reduced_len in reversed(range(len(iterables))):
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            nexts = cycle(islice(nexts, reduced_len))
            # This skips one round of the cycle, starting the next round
            # without the current iter


The last comment is probably the least valuable, though it does point out the 
slight quirk of how the last line works. I suppose this is the case for having 
the loop variable be named the actual length, but I think most Python users are 
accustomed to the last element of a list having index length-1. At any rate, I 
think the readability gain in the for statement is more than the readability 
loss in the islice().

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32099>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to