Interesting list() un-optimization
Roy Smith
roy at panix.com
Sun Mar 10 09:05:09 EDT 2013
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, but it's logical and
experimentally, it seems to work.
Can anybody see any downside to this?
More information about the Python-list
mailing list