[Python-ideas] An exhaust() function for iterators

David Mertz mertz at gnosis.cx
Fri Oct 11 20:51:20 CEST 2013


It is hard to imagine that doing this:

  for _ in side_effect_iter: pass

Could EVER realistically spend a significant share of its time in the loop
code.  Side effects almost surely need to do something that vastly
overpowers the cost of the loop itself (maybe some I/O, maybe some
computation), or there's no point in using a side-effect iterator.

I know you *could* technically write:

  def side_effect_iter(N, obj):
      for n in range(N):
          obj.val = n
          yield True

And probably something else whose only side effect was changing some value
that doesn't need real computation.  But surely writing that and exhausting
that iterator is NEVER the best way to code such a thing.

On the other hand, a more realistic one like this:

  def side_effect_iter(N):
      for n in range(N):
          val = complex_computation(n)
          write_to_slow_disk(val)
          yield True

Is going to take a long time in each iteration, and there's no reason to
care that the loop isn't absolutely optimal speed.



On Fri, Oct 11, 2013 at 11:29 AM, Neil Girdhar <mistersheik at gmail.com>wrote:

> This was also my thought.
>
>
> On Sunday, September 29, 2013 4:42:20 PM UTC-4, Serhiy Storchaka wrote:
>
>> 29.09.13 07:06, Clay Sweetser написав(ла):
>> > I would like to propose that this function, or one very similar to it,
>> > be added to the standard library, either in the itertools module, or
>> > the standard namespace.
>> > If nothing else, doing so would at least give a single *obvious* way
>> > to exhaust an iterator, instead of the several miscellaneous methods
>> > available.
>>
>> I prefer optimize the for loop so that it will be most efficient way (it
>> is already most obvious way).
>>
>> ______________________________**_________________
>> Python-ideas mailing list
>> Python... at python.org
>> https://mail.python.org/**mailman/listinfo/python-ideas<https://mail.python.org/mailman/listinfo/python-ideas>
>>
>>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131011/127acc9f/attachment.html>


More information about the Python-ideas mailing list