[Python-Dev] length_hint (original) (raw)

Daniel Stutzbach daniel at stutzbachenterprises.com
Fri Apr 3 01:14:59 CEST 2009


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 <http://stutzbachenterprises.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20090402/c714b7d1/attachment.htm>



More information about the Python-Dev mailing list