[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)
Terry Reedy tjreedy at udel.edu
Sun Jul 15 03:03:31 CEST 2012
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]