Interesting list() un-optimization
Terry Reedy
tjreedy at udel.edu
Sun Mar 10 17:39:14 EDT 2013
On 3/10/2013 9:05 AM, Roy Smith wrote:
> In article <roy-572C99.22201106032013 at 70-1-84-166.pools.spcsdns.net>,
> Roy Smith <roy at panix.com> wrote:
>
>> The problem is, QuerySets have a __len__() method. Calling it is a lot
>> faster than iterating over the whole query set and counting the items,
>> but it does result in an additional database query, which is a lot
>> slower than the list resizing! Writing the code as a list comprehension
>> prevents list() from trying to optimize when it shouldn't!
>
> Hmmm, I think I've found a good solution.
>
> It turns out, we don't actually use QuerySet in our models. We've
> defined our own QuerySet subclass which adds a few convenience methods.
> Adding
>
> def __len__(self):
> raise NotImplemented
>
> to our subclass should do the job. It looks like list() respects that,
> calls __iter__(), and does the right thing. I can't find any place
> where that behavior for list() is documented,
It is a cpython implementation detail that probably has changed with the
versions.
> but it's logical and experimentally, it seems to work.
> Can anybody see any downside to this?
No. Give it a try.
--
Terry Jan Reedy
More information about the Python-list
mailing list