[Python-ideas] Use the plus operator to concatenate iterators

Chris Angelico rosuav at gmail.com
Wed Aug 5 04:44:57 CEST 2015


On Wed, Aug 5, 2015 at 12:37 PM,  <random832 at fastmail.us> wrote:
> On Tue, Aug 4, 2015, at 21:34, Chris Angelico wrote:
>> Almost completely useless. Multiplying an *iterable* by an integer
>> will often be useful (eg multiplying list by int), but multiplying an
>> *iterator* (or even just adding one to itself) is going to be useless,
>> because the first time through it will exhaust it, and any
>> well-behaved iterator will remain exhausted once it's ever raised
>> StopIteration. (Note that the 'class iter' that I posted earlier is
>> NOT well-behaved. You can add something onto an exhausted iterator and
>> rejuvenate it. It'd take a couple extra lines of code to fix that.)
>
> Suppose iterator * number returns a new iterator which will iterate
> through the original iterator once, caching the results, and then yield
> the cached results n-1 times.

That's easily spelled "list(iterator) * number", apart from the fact
that it makes a concrete result list. I don't think it needs language
support.

ChrisA


More information about the Python-ideas mailing list