Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'method.': 0.05; 'versions.': 0.07; 'convenience': 0.09; 'counting': 0.09; 'models.': 0.09; 'prevents': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'terry': 0.09; 'def': 0.10; 'downside': 0.16; 'iterating': 0.16; 'query,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:optimization': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'solution.': 0.18; 'trying': 0.21; 'logical': 0.22; 'defined': 0.22; 'work.': 0.23; "i've": 0.23; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'header:X -Complaints-To:1': 0.28; 'this?': 0.28; 'subject:list': 0.28; 'methods.': 0.29; 'respects': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'query': 0.30; 'code': 0.31; 'anybody': 0.32; 'turns': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'that,': 0.34; "can't": 0.34; 'changed': 0.34; 'list': 0.35; 'adds': 0.35; 'faster': 0.35; 'received:org': 0.36; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'detail': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'skip:n 10': 0.63; 'behavior': 0.64; 'our': 0.65; 'smith': 0.71; 'article': 0.78; 'received:fios.verizon.net': 0.84; 'items,': 0.91; 'try.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Interesting list() un-optimization Date: Sun, 10 Mar 2013 17:39:14 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362951587 news.xs4all.nl 6889 [2001:888:2000:d::a6]:42796 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41038 On 3/10/2013 9:05 AM, Roy Smith wrote: > In article , > Roy Smith 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