[Python-Dev] length_hint (original) (raw)
Raymond Hettinger python at rcn.com
Fri Apr 3 01:30:39 CEST 2009
- Previous message: [Python-Dev] __length_hint__
- Next message: [Python-Dev] UnicodeDecodeError bug in distutils
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Iterators can implement a method called lengthhint that provides a hint to certain internal routines (such as list.extend) so they can operate more efficiently. As far as I can tell, lengthhint is currently undocumented. Should it be? This has been discussed, and no, it is a implementation detail mostly for the optimization of builtin iterators.
Right. That matches my vague recollection on the subject.
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
Yes.
BTW, the same rule also applies to len. IIRC, Tim proposed to add that to the docs somewhere.
Perhaps Raymond can shed some light on these.
Can't guess the future of length_hint(). Since it doesn't have a slot, the attribute lookup can actually slow down cases with a small number of iterands.
The original idea was based on some research on map/fold operations, noting that iterators can sometimes be processed more efficiently if accompanied by some metadata (i.e. the iterator has a known length, consists of unique items, is sorted, is all of a certain type, is re-iterable, etc.).
Raymond
- Previous message: [Python-Dev] __length_hint__
- Next message: [Python-Dev] UnicodeDecodeError bug in distutils
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]