[Python-Dev] __length_hint__
Benjamin Peterson
benjamin at python.org
Fri Apr 3 01:17:08 CEST 2009
2009/4/2 Daniel Stutzbach <daniel at stutzbachenterprises.com>:
> Iterators can implement a method called __length_hint__ that provides a hint
> to certain internal routines (such as list.extend) so they can operate more
> efficiently. As far as I can tell, __length_hint__ is currently
> undocumented. Should it be?
This has been discussed, and no, it is a implementation detail mostly
for the optimization of builtin iterators.
>
> If so, are there any constraints on what an iterator should return? I can
> think of 3 possible rules, each with advantages and disadvantages:
> 1. return your best guess
> 2. return your best guess that you are certain is not higher than the true
> value
> 3. return your best guess that you are certain is not lower than the true
> value
>
> Also, I've noticed that if a VERY large hint is returned by the iterator,
> list.extend will sometimes disregard the hint and try to allocate memory
> incrementally (correct for rule #1 or #2). However, in another code path it
> will throw a MemoryError immediately based on the hint (correct for rule
> #3).
Perhaps Raymond can shed some light on these.
--
Regards,
Benjamin
More information about the Python-Dev
mailing list