Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1362673269; bh=ANGWokTUDMQ+sw9o8Vml+BnN47FvLuYntFmJjFFKWF8=; h=To:From:Subject:Date:Message-ID:References:Mime-Version: Content-Type:Content-Transfer-Encoding:In-Reply-To; b=gYoOKiwdO3WtZgsreyKooIqPmj5wq5anZZ2oUKrXKVkqQtSoMLMikSTdm81k0a3Lj V9PpSxofZlML9LKkxOGikW06ULgi2lY7G/j3QOpdHRTnQ7qwppX2YyiFK0p5Xqe+ee XOEITOqACD71vEgU57wUBas6ghcj7II1NDZIq/Og= X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; "object's": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'url:peps': 0.09; 'from:name:christian heimes': 0.16; 'hint': 0.16; 'iterators': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skips': 0.16; 'subject:optimization': 0.16; 'from:addr:python.org': 0.17; 'wrote:': 0.17; 'thu,': 0.17; 'url:dev': 0.17; '>>>': 0.18; '"",': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:list': 0.28; '>>>>': 0.29; 'methods.': 0.29; "skip:' 10": 0.30; 'url:python': 0.32; 'file': 0.32; 'generally': 0.32; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'christian': 0.34; 'too.': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'uses': 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; 'header:Received:5': 0.40; 'your': 0.60; '2013': 0.84; 'costly': 0.84; 'hood': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: Interesting list() un-optimization Date: Thu, 07 Mar 2013 17:20:52 +0100 References: <20130306215735.649932ee@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: smtp.semantics.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362673271 news.xs4all.nl 6849 [2001:888:2000:d::a6]:43277 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40799 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/ Christian