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 X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; 'raises': 0.07; 'works.': 0.07; '[1,': 0.09; 'behavior,': 0.09; "object's": 0.09; 'pep': 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; 'def': 0.10; 'hint': 0.16; 'iterators': 0.16; 'message-id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'subject:optimization': 0.16; 'why,': 0.16; 'integer': 0.17; 'stefan': 0.17; 'tries': 0.17; 'url:dev': 0.17; '>>>': 0.18; 'question.': 0.20; '"",': 0.22; 'skip:_ 20': 0.22; 'header:User- Agent:1': 0.26; '(most': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:list': 0.28; 'falls': 0.29; 'methods.': 0.29; 'case,': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'checked': 0.30; 'url:python': 0.32; 'file': 0.32; 'generally': 0.32; 'could': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'that,': 0.34; 'skip:l 30': 0.35; 'doing': 0.35; 'too.': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'best,': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'first': 0.61; 'back': 0.62; 'provide': 0.62; 'skip:n 10': 0.63 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Wolfgang Maier Subject: Re: Interesting list() un-optimization Date: Thu, 7 Mar 2013 20:41:37 +0000 (UTC) References: <20130306215735.649932ee@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 77.2.132.155 (Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0) 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362688920 news.xs4all.nl 6931 [2001:888:2000:d::a6]:57002 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40823 > >>> 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/ > > very interesting (hadn't heard of it)! Just checked the PEP, then tested list()'s behavior, and it is just as described: class stupid(list): def __len__(self): print ('len() called') return NotImplemented def __length_hint__(self): print ('hint requested') l=iter(self).__length_hint__() print (l) return l a=stupid((1,2,3)) len(d) ======> len() called Traceback (most recent call last): File "", line 1, in len(d) TypeError: an integer is required list(d) ======> len() called hint requested 3 [1, 2, 3] so list() first tries to call the iterable's __len__ method. If that raises a TypeError it falls back to __length_hint__ . What I still don't know is how the listiterator object's __length_hint__ works. Why, in this case, does it know that it has a length of 3 ? The PEP does not provide any hint how a reasonable hint could be calculated. > And how exactly would it do that, without either doing what __len__ does or > reading the whole result set into memory? > > Stefan > a very good question. Best, Wolfgang