[Python-Dev] PEP 0424: A method for exposing a length hint
Stephen J. Turnbull
stephen at xemacs.org
Mon Jul 16 05:52:18 CEST 2012
Nick Coghlan writes:
> Right, I agree on the value in being able to return something to say "this
> cannot be converted to a concrete container".
> I still haven't seen a use case where the appropriate response to "I don't
> know" differs from the appropriate response to a hint of zero - that is,
> you don't preallocate, you just start iterating.
Why wouldn't one just believe the hint and jump past the iteration?
What about an alternative API such as length_hint(iter, bound)
returning 'cannot say' (if no hint is available), 'small' (if the
estimated length is less than bound), and 'large' (if it's greater
than the bound or infinite)? (Or None, True, False which would give
the boolean interpretation "do I know I'm small enough to be converted
to a concrete container?")
The point is that I don't really see the value in returning a precise
estimate that cannot be relied on to be accurate. OK, Python is a
"consenting adults" language, but returning an integer here seems like
invitation to abuse.
More information about the Python-Dev