Interesting list() un-optimization

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 7 21:26:59 CET 2013


On Thu, Mar 7, 2013 at 12:19 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
>> 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?

If the underlying cursor provides its own efficient length hint, it
could return that.  Or if the query is result-limited, use the limit
as a length hint, provided it's not absurdly large.  And if you really
can't efficiently determine anything about the length of the result
set at all, you can always fall back on returning NotImplemented.



More information about the Python-list mailing list