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

Stefan Behnel stefan_ml at behnel.de
Mon Jul 16 09:10:45 CEST 2012

Antoine Pitrou, 15.07.2012 17:06:
> On Sun, 15 Jul 2012 16:33:23 +0200
> Christian Heimes wrote:
>> Am 15.07.2012 16:22, schrieb Antoine Pitrou:
>>> On Mon, 16 Jul 2012 00:08:41 +1000
>>> Nick Coghlan wrote:
>>>> Right, I agree on the value in being able to return something to say "this
>>>> cannot be converted to a concrete container".
>>> Who would be able to return that, apart from trivial cases like
>>> itertools.cycle()?
>> For example most numerical sequence iterators like Fibonacci generator,
>> prime number sequence generator and even trivial cases like even natural
>> number generator.
> First, you can't implement __length_hint__ for a generator, which is the
> preferred (the most practical) way of writing iterators in pure Python.

It can be implemented for generator expressions without a conditional,
though, including the case of comprehensions. I wanted to do this in Cython
for a while, but the protocol wasn't very well adapted to that use case.
The "don't know" case was just too common and inefficient.

For the other points, I agree with the already presented counterarguments.
Being able to prevent some obvious traps is a good thing, even if you can't
prevent all of them.


More information about the Python-Dev mailing list