[Python-Dev] PEP 0424: A method for exposing a length hint

Antoine Pitrou solipsis at pitrou.net
Sun Jul 15 14:36:41 CEST 2012

On Sun, 15 Jul 2012 18:47:38 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> > * __length_hint__ should be allowed to return None to indicate "don't know"
> >   or -1 to indicate "infinite".
> >
> > Presumably anything that wishes to create a list or other sequence from an
> > object with a hint of -1 could then raise an exception immediately.
> I'm not seeing the value in returning None over 0 for the don't know
> case - it just makes the API harder to use.

The point is that 0 is a legitimate value for a length hint. Simple
implementations of __length_hint__ will start returning 0 as a
legitimate value and you will wrongly interpret that as "don't know",
which kinds of defeat the purpose of __length-hint__ ;)

That said, I don't think a special value for "is infinite" is useful.
Just make -1 mean "I don't know".



More information about the Python-Dev mailing list