[Python-Dev] Do we need __length_hint__ at all? (Was PEP 0424: A method for exposing a length hint)

Antoine Pitrou solipsis at pitrou.net
Mon Jul 16 16:40:04 CEST 2012


On Tue, 17 Jul 2012 00:18:55 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> Given that all standard library containers default to assuming a size
> of zero (absent information indicating otherwise), and a special value
> would need to be special cased by *every* client of the API (and
> almost always defaulted to zero), it's simply not a good trade-off.

Actually, dict and set default to a non-zero internal size, but I agree
making containers harder to implement is not a good thing.

> >> The PEP itself already has this general tone, but I think that it
> >> should be even more emphatic that it's about codifying the status quo,
> >> *not* about modifying it with ideas haven't been proven useful through
> >> past experience.
> >
> > Then the PEP shouldn't address infinite iterators at all.
> 
> Noting that infinite iterators are free to define __length_hint__ to
> always throw an exception *is* the status quo.

Being "free" to do unexpected or unconventional things is not the same
thing as codifying those behaviours in a PEP, especially when noone is
actually doing them. __length_hint__ is supposed to be informative, it
shouldn't error out on you. So still -1 from me.

Regards

Antoine.


More information about the Python-Dev mailing list