Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.05; 'python': 0.07; 'all?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'functools': 0.16; 'hitting': 0.16; 'lambda': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:Pythonic': 0.16; 'writes:': 0.20; 'code': 0.22; 'loop': 0.22; 'right,': 0.22; 'away.': 0.23; 'interpreted': 0.23; 'once.': 0.23; 'skip:( 30': 0.24; 'creating': 0.26; "i'm": 0.26; 'chris': 0.27; 'subject:?': 0.29; "won't": 0.30; 'from:addr:web.de': 0.31; 'paul': 0.32; "skip:' 10": 0.32; 'import': 0.32; 'to:addr:python-list': 0.32; 'bit': 0.33; 'header:X-Complaints-To:1': 0.34; 'occurs': 0.35; 'right?': 0.35; 'some': 0.37; 'should': 0.37; 'faster': 0.38; 'but': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime- Version:1': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'best': 0.60; 'dealing': 0.71; 'evaluate': 0.72; 'ugly,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Pythonic infinite for loop? Date: Fri, 15 Apr 2011 10:11:39 +0200 Organization: None References: <7xsjtkx9hy.fsf@ruckus.brouhaha.com> <7xfwpkq7l5.fsf@ruckus.brouhaha.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849b88.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 41 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302855113 news.xs4all.nl 65870 [::ffff:82.94.164.166]:36713 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3250 Paul Rubin wrote: > Chris Angelico writes: >>> sentinel = object() >>> seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1)) >>> lst = list(takewhile(lambda x: x != sentinel, seq)) >> >> If I understand this code correctly, that's creating generators, >> right? It won't evaluate past the sentinel at all? > > Right, it should stop after hitting the sentinel once. > >> That might well be what I'm looking for. A bit ugly, but efficient and >> compact. And I can bury some of the ugliness away. > > It occurs to me, operator.ne might be a little faster than the > interpreted lambda. Or operator.is_not as you are dealing with a singleton. You also need functools.partial: $ python -m timeit -s'sentinel = object(); predicate = lambda x: x != sentinel' 'predicate(None)' 1000000 loops, best of 3: 0.369 usec per loop $ python -m timeit -s'sentinel = object(); predicate = lambda x: x is not sentinel' 'predicate(None)' 1000000 loops, best of 3: 0.314 usec per loop $ python -m timeit -s'from functools import partial; from operator import ne; sentinel = object(); predicate = partial(ne, sentinel)' 'predicate(None)' 1000000 loops, best of 3: 0.298 usec per loop $ python -m timeit -s'from functools import partial; from operator import is_not; sentinel = object(); predicate = partial(is_not, sentinel)' 'predicate(None)' 1000000 loops, best of 3: 0.252 usec per loop