[Python-ideas] Proposal: allow length_hint to specify infinite iterators
Antoine Pitrou
solipsis at pitrou.net
Wed Nov 29 06:07:24 EST 2017
On Wed, 29 Nov 2017 09:39:57 +0200
Serhiy Storchaka <storchaka at gmail.com>
wrote:
> 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.
Not to mention that many infinite iterators cannot be predicted in
advance to be infinite. Only the more trivial cases such as count()
would apply (but not a takewhile() involving count(), for example).
Regards
Antoine.
More information about the Python-ideas
mailing list