[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)

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:

> * lengthhint 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".

Regards

Antoine.



More information about the Python-Dev mailing list