In article <roy-572c99.22201106032...@70-1-84-166.pools.spcsdns.net>,
 Roy Smith <r...@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, but it's logical and 
experimentally, it seems to work.

Can anybody see any downside to this?
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to