Interesting list() un-optimization

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 7 12:31:44 EST 2013


On Thu, Mar 7, 2013 at 9:20 AM, Christian Heimes <christian at python.org> wrote:
> Am 07.03.2013 17:00, schrieb Ian Kelly:
>> On Thu, Mar 7, 2013 at 4:22 AM, Wolfgang Maier
>> <wolfgang.maier at biologie.uni-freiburg.de> 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,



More information about the Python-list mailing list