count items in generator

Delaney, Timothy (Tim) tdelaney at avaya.com
Mon May 15 04:35:35 CEST 2006


George Sakkis wrote:

> Delaney, Timothy (Tim) wrote:
>> 
>> list(itertools.count()) will eventually fail with a MemoryError.
> 
> That's more of a theoretical argument on why the latter is worse. How
> many real-world programs are prepared for MemoryError every time they
> call list(), catch it and handle it graciously ? I'd say that the only
> reason an exception would be preferable in such case would be
> debugging; it's nice to have an informative traceback instead of a
> program that entered an infinite loop.

That's exactly my point. Assuming your test coverage is good, such an
error would be caught by the MemoryError. An infinite loop should also
be caught by timing out the tests, but that's much more dependent on the
test harness.

Tim Delaney



More information about the Python-list mailing list