Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Early return is definitely needed. I guess you could make bare `return` early return, and `return None` the way to yield None, but that's kind of confusing.


I agree that would be confusing, but I question that early return is needed - I don't think I've ever seen a generator example where early return is used. It could be that it's because I haven't seen enough examples of course. Please share some examples of a generator with early return which can't be accomplished easily and clearly in some other way.


    def first10(*iterables):
        n = 0
        for iterable in iterables:
            for item in iterable:
                yield item
                n += 1
                if n == 10:
                    return


This is actually a one-liner, because itertools is awesome:

  first10 = lambda iterables : itertools.islice(itertools.chain(*iterables), 0, 10)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: