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

Nick Coghlan ncoghlan at gmail.com
Mon Jul 16 06:20:17 CEST 2012


On Jul 16, 2012 1:52 PM, "Stephen J. Turnbull" <stephen at xemacs.org> wrote:
> The point is that I don't really see the value in returning a precise
> estimate that cannot be relied on to be accurate.  OK, Python is a
> "consenting adults" language, but returning an integer here seems like
> invitation to abuse.

Because preallocating memory is ridiculously faster than doing multiple
resizes. That's all this API is for: how many objects should a container
constructor preallocate space for when building from an iterable. It's an
important optimisation in CPython when using itertools, and PyPy is
planning to adopt it as well. Alex is doing the right thing in attempting
to standardise it rather than risk the two implementations using subtly
incompatible definitions.

Skipping the iteration in the zero case is a pointless micro-optimisation
that just makes the API more complex for no good reason. Allowing a
negative hint to mean "infinite", on the other hand, avoids certain
categories of errors without making the API any harder to use (since
negatives have to be rejected anyway).

--
Sent from my phone, thus the relative brevity :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120716/79bf6cc7/attachment.html>


More information about the Python-Dev mailing list