(original) (raw)

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?


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).

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC