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

Mark Shannon mark at hotpy.org
Wed Aug 1 10:46:35 CEST 2012


While the idea behind PEP 424 is sound, the text of the PEP is rather vague and missing a lot of details.
There was extended discussion on the details, but none of that has appeared in the PEP yet.

So Alex, how about adding those details?

Also the rationale is rather poor.
Given that CPython is the reference implementation, PyPy should be compared to CPython, not vice-versa.
Reversing PyPy and CPython in the rationale gives:

'''
Being able to pre-allocate lists based on the expected size, as estimated by __length_hint__,
can be a significant optimization.
PyPy has been observed to run some code slower than CPython, purely because this optimization is absent.
'''

Which is a PyPy bug report, not a rationale for a PEP ;)

Perhaps a better rationale would something along the lines of:

'''
Adding a __length_hint__ method to the iterator protocol allows sequences, notably lists,
to be initialised from iterators with only a single resize operation.
This allows sequences to be intialised quickly, yet have a small growth factor, reducing memory use.
'''

Cheers,
Mark.


More information about the Python-Dev mailing list