Interesting list() un-optimization

Roy Smith roy at panix.com
Sun Mar 10 18:34:58 EDT 2013


In article <mailman.3167.1362951587.2939.python-list at python.org>,
 Terry Reedy <tjreedy at udel.edu> wrote:

> > 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.

Yeah, that's what I was afraid of.  The "obvious" solution of:

class QuerySet(mongoengine.queryset.QuerySet):
    def __init__(self, document, collection):
        super(QuerySet, self).__init__(document, collection)
        [...]
        del self.__len__

results in:

[rest of stack dump elided]
    del self.__len__
AttributeError: __len__

which I don't understand.



More information about the Python-list mailing list