[Python-Dev] Idea: __length_hint__ wrapper in itertools

Carlos Pita carlosjosepita at gmail.com
Thu Feb 19 18:15:32 CET 2015


Hi all,

python now supports the __length_hint__ method but not for every use
case that could potentially benefit from it, v.g.:

1) Some common builtins like map don't set the hint.
2) Generators.
3) Application specific hints that, by definition, can't be predicted
by a general strategy.

I know 1 and 2 were discussed at some length in the past, but still
there is no agreement about what to do with them.

In the meantime, and because of 3, what do you think about adding a
simple iterator wrapper to itertools which would allow to provide the
hint by the user without any significant performance lost (__next__ in
the wrapper could be just the original __next__). AFAIK there is no
such thing in the standard library or anywhere else and it seems
pretty easy to implement, although maybe I'm completely wrong.

Say:

hinted_iter = it.length_hint(iter, hint)

Cheers
--
Carlos


More information about the Python-Dev mailing list