[Python-ideas] Proposal: allow length_hint to specify infinite iterators

Serhiy Storchaka storchaka at gmail.com
Wed Nov 29 02:39:57 EST 2017


29.11.17 03:34, Steven D'Aprano пише:
> This wastes the opportunity to fail fast on operations which cannot
> possibly succeed, e.g. list(count()) must eventually fail with
> MemoryError. Or worse: if the OS starts thrashing trying to meet the
> memory requests, you can lock up the computer.
> 
> I propose that we:
> 
> (1) extend the __length_hint__ protocol to allow iterators to report
> that they are infinite;
> 
> (2) and recommend that consumers of iterators (such as list) that
> require finite input should fail fast in the event of an infinite
> iterator.

Infinite iterators are rare. And count() is even more special in the 
sense that iterating it doesn't have side effects and is not interruptible.

> (c) take the convention that returning sys.maxint means infinity;

Returning sys.maxsize will likely lead to failing fast with MemoryError.



More information about the Python-ideas mailing list