[Python-ideas] Aid reiteration with new class: gfic
john.a.graham at gmail.com
Sat Jun 20 05:55:42 CEST 2009
Mathias Panzenböck wrote:
> Paul Moore wrote:
>> 2009/6/19 John Graham <john.a.graham at gmail.com>:
>>> Exactly how does itertools.tee not cover this use case?
>> I've been wondering the same sort of thing. But if you're going to
>> sequentially run through the iterator twice
>> for i in it:
>> do something
>> for i in it:
>> do something else
>> then itertools.tee is documented as being basically equivalent to, but
>> worse than, list(): "In general, if one iterator is going to use most
>> or all of the data before the other iterator, it is faster to use
>> list() instead of tee()".
>> So the question becomes, how does list() not cover this use case.
>> The only answer I can see is that the proposal avoids allocating
>> temporary storage for all of the values generated. Consider
>> xrange(1000000) - if you can call xrange twice, rather than saving a
>> million values, you've saved a lot of memory.
>> But this only applies when you're writing functions designed to take
>> extremely general iterators. Someone writing a function which can take
>> essentially arbitrary iterators, and which has to handle unboundedly
>> large input gracefully, probably has worse problems than the lack of
>> standard library support for something they can code themselves fairly
> I don't know if this applies here but generators can also yield
> infinite lists. In such an case list() is not an alternative.
Hrm, that leads to the question, is it mathematically possible to
iterate over an infinite list... twice? :)
More information about the Python-ideas