[Python-Dev] __length_hint__

Raymond Hettinger python at rcn.com
Fri Apr 3 01:30:39 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?
> 
> 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






More information about the Python-Dev mailing list