This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dubslow
Recipients Dubslow, docs@python, rhettinger, terry.reedy
Date 2017-11-21.01:50:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1511229004.82.0.213398074469.issue32099@psf.upfronthosting.co.za>
In-reply-to
Content
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().
History
Date User Action Args
2017-11-21 01:50:04Dubslowsetrecipients: + Dubslow, rhettinger, terry.reedy, docs@python
2017-11-21 01:50:04Dubslowsetmessageid: <1511229004.82.0.213398074469.issue32099@psf.upfronthosting.co.za>
2017-11-21 01:50:04Dubslowlinkissue32099 messages
2017-11-21 01:50:03Dubslowcreate