[Python-Dev] PEP 0424: A method for exposing a length hint

Terry Reedy tjreedy at udel.edu
Sun Jul 15 03:03:31 CEST 2012


On 7/14/2012 6:11 PM, Alex Gaynor wrote:
...

Various thoughts:

"This method is then used by various other functions (such +as ``map``) 
to presize lists"
  -- map no longer produces lists. This only makes sense in 3.x if you 
mean that map can pass along the value of its inputs.

"Types can then define ``__length_hint__`` which are not
+sized, and thus should not define ``__len__``,"
is awkwardly phrased. I think you mean
"Types that are not sized and should not define __len__ can then define 
__length_hint__.

What do 'sized' and 'should' mean? Some iterators know exactly how many 
items they have yet to yield. The main implication of having a __len__ 
versus __length_hint__ methods seems to be it bool() value when empty.

If lists were to get a new keyword arg, so should the other classes 
based on one internal array. I see this has been removed.

Generator functions are the nicest way to define iterators in Python. 
Generator instances returned from generator functions cannot be given a 
length hint. They are not directly helped. However ...

Not addressed in the PEP: do consumers of __length_hint look for it (and 
__length__ before or after calling iter(input), or both? If before, then 
the following should work.

class gwlh: # generator with length hint
     def __init__(self, gen, len):
         self.gen = gen
         self.len = len
     def __iter__(self):
         return self.gen
     def __length_hint__(self):
         return len

Do transformation iterators pass through hints from inputs? Does map(f, 
iterable) look for len or hint on iterable? Ditto for some itertools, 
like chain (add lengths). Any guidelines in the PEP

-- 
Terry Jan Reedy





More information about the Python-Dev mailing list