Interesting list() un-optimization

Terry Reedy tjreedy at udel.edu
Thu Mar 7 15:34:33 EST 2013


On 3/7/2013 11:20 AM, Christian Heimes wrote:

> But iterators have a length hint method that are used for some
> optimizations and preallocations, too.

This is easy when the base iterable has a length method, as do range 
objects.

>>>> i = iter(range(10))
>>>> i.__length_hint__()
> 10

And the length_hint can (should be) decremented with each next call.

 >>> next(i); next(i)
0
1
 >>> i.__length_hint__()
8

-- 
Terry Jan Reedy




More information about the Python-list mailing list