Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'method.': 0.05; "object's": 0.09; 'typeerror:': 0.09; 'underlying': 0.09; 'url:peps': 0.09; '9:20': 0.16; 'hint': 0.16; 'iterators': 0.16; 'skips': 0.16; 'subject:optimization': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'url:dev': 0.17; '>>>': 0.18; 'thanks.': 0.21; '"",': 0.22; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; '>>>>': 0.29; 'methods.': 0.29; 'skip:_ 10': 0.29; "skip:' 10": 0.30; 'implement': 0.32; 'url:python': 0.32; 'file': 0.32; 'generally': 0.32; 'could': 0.32; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'christian': 0.34; 'too.': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; "didn't": 0.36; 'method': 0.36; 'uses': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'your': 0.60; 'manner': 0.74; '2013': 0.84; 'costly': 0.84; 'hood': 0.84; 'presumably': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=TCmLW5pOPTDN3/E6tms1BiZjVZvZ+bC2g8P/z1p6qkM=; b=FN3moDVQmxgW5+utz9WI24c1EdQcC6BkpohosvBAO7u5t/NM9JvFpROYlnc0zk27bl xrofbNnHFr1Jq3LQfrlBwp74TtE6ai6C6dPfkoTJf90tdcMV1YgUq5MvjIDk+3Cn6KIL mMLTuu5F4HZ/8viucUDfuI+k11wYtgg/aYNMYmDzxcpyEDPxndmdENW/QIHVOtArvjSq TB0OyfMql7CHLMrEqRZmdF5V5bbgYQCDTfDAo/vB9nNCQw/RzX7pWCwQ3rPFLLxM5PPu ypJ094zC79+m8if6of05mV7UwiDZknG1+TLSsH1pCItgsswkAB8NOeW8gKt/1u00jxzx tyAA== X-Received: by 10.52.177.163 with SMTP id cr3mr11646540vdc.94.1362677544681; Thu, 07 Mar 2013 09:32:24 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <20130306215735.649932ee@bigbox.christie.dr> From: Ian Kelly Date: Thu, 7 Mar 2013 10:31:44 -0700 Subject: Re: Interesting list() un-optimization To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362677547 news.xs4all.nl 6948 [2001:888:2000:d::a6]:44170 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40804 On Thu, Mar 7, 2013 at 9:20 AM, Christian Heimes wrote: > Am 07.03.2013 17:00, schrieb Ian Kelly: >> On Thu, Mar 7, 2013 at 4:22 AM, Wolfgang Maier >> wrote: >>> Well, it skips the costly len() call because your iter(Foo()) returns >>> iter(range()) under the hood and list() uses that object's __len__() method. >> >> Iterators do not generally have __len__ methods. >> >>>>> len(iter(range(10))) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: object of type 'range_iterator' has no len() > > But iterators have a length hint method that are used for some > optimizations and preallocations, too. > >>>> i = iter(range(10)) >>>> i.__length_hint__() > 10 > > See http://www.python.org/dev/peps/pep-0424/ Didn't know about that, thanks. Presumably a proper iter(QuerySet()) object could implement __length_hint__ in an efficient manner rather than by just calling the __len__ of the underlying QuerySet,