Interesting list() un-optimization

Stefan Behnel stefan_ml at behnel.de
Thu Mar 7 14:19:07 EST 2013


Ian Kelly, 07.03.2013 18:31:
> On Thu, Mar 7, 2013 at 9:20 AM, Christian Heimes wrote:
>> Am 07.03.2013 17:00, schrieb Ian Kelly:
>>> On Thu, Mar 7, 2013 at 4:22 AM, Wolfgang Maier wrote:
>>>> Well, it skips the costly len() call because your iter(Foo()) returns
>>>> iter(range()) under the hood and list() uses that object's __len__() method.
>>>
>>> Iterators do not generally have __len__ methods.
>>>
>>> >>> len(iter(range(10)))
>>> Traceback (most recent call last):
>>>   File "<stdin>", line 1, in <module>
>>> TypeError: object of type 'range_iterator' has no len()
>>
>> But iterators have a length hint method that are used for some
>> optimizations and preallocations, too.
>>
>> >>> i = iter(range(10))
>> >>> i.__length_hint__()
>> 10
>>
>> See http://www.python.org/dev/peps/pep-0424/
> 
> Didn't know about that, thanks.  Presumably a proper iter(QuerySet())
> object could implement __length_hint__ in an efficient manner rather
> than by just calling the __len__ of the underlying QuerySet,

And how exactly would it do that, without either doing what __len__ does or
reading the whole result set into memory?

Stefan





More information about the Python-list mailing list