Interesting list() un-optimization

Terry Reedy tjreedy at
Sun Mar 10 22:39:14 CET 2013

On 3/10/2013 9:05 AM, Roy Smith wrote:
> In article <roy-572C99.22201106032013 at>,
>   Roy Smith <roy at> 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 

> 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