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

Nick Coghlan ncoghlan at gmail.com
Tue Jul 17 05:19:55 CEST 2012

On Tue, Jul 17, 2012 at 1:03 PM, Alex Gaynor <alex.gaynor at gmail.com> wrote:
> I've updated the PEP to reflect the discussion. There are two major changes:
> 1) NotImplemented may be used by __length_hint__ to indicate that there is no
>    finite length hint available.

I've been thinking about this a bit more, and I think it does provide
good scope for eventually adding __length_hint__ to more iterators
(including map, filter and enumerate).

> 2) callers of operator.length_hint() must provide their own default value, this
>    is also required by the current C _PyObject_LengthHint implementation.

And this makes it explicit that API users need to deal with the
AttributeError/NotImplemented case, whilst making it easy to do so.
Good call.

> There are no provisions for infinite iterators, that is not within the scope of
> this proposal.

I'll repeat my observation that remaining silent on this point is
effectively identical to blessing the practice of raising an exception
in __length_hint__ to force fast failure of attempts to convert an
infinite iterator to a concrete container. Rather than leaving people
to figure this out on their own, we may as well make it official that
TypeError can be raised in __length_hint__ to block conversion to
concrete containers that use a preallocation strategy.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list