Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'raised': 0.07; 'terry': 0.07; 'python': 0.08; 'loop.': 0.09; 'am,': 0.12; 'def': 0.15; 'case.': 0.15; "'',": 0.16; '3.2,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'reedy': 0.16; 'terminates': 0.16; 'mon,': 0.16; 'wrote:': 0.16; '>>>': 0.18; '(most': 0.21; 'header:In-Reply-To:1': 0.22; 'body.': 0.23; 'breaks': 0.23; 'sep': 0.23; 'traceback': 0.24; 'code': 0.25; 'saying': 0.26; 'compare': 0.28; 'loop': 0.28; 'yield': 0.29; 'message-id:@mail.gmail.com': 0.29; 'skip:% 10': 0.30; 'least': 0.31; "isn't": 0.33; 'to:addr:python-list': 0.33; 'last):': 0.34; 'subject:next': 0.34; 'file': 0.36; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'received:209.85.218.46': 0.91; 'received :mail-yi0-f46.google.com': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=9BKza4rK7O08kzh6Ib3b/61XMb1Rj05q5EzainndqYM=; b=YUMRBJ119+BOJvx1YFLDkfmuI2AE4WYaGkmh9Jg7WmrjDTwJMd19IfSkuVxDsEuOcv 8SQpKyOjDarpcXevR4LGqQ3m1VdW+vK3XcHZ+zVCuDxxURXycvBZuYNsvbXP6Q968yIW KBa9cilDzKIvmAumLSsiNFr83XBAWL4TfPPQc= MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 12 Sep 2011 08:41:05 +1000 Subject: Re: Idioms combining 'next(items)' and 'for item in items:' From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315780869 news.xs4all.nl 2507 [2001:888:2000:d::a6]:42363 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13151 On Mon, Sep 12, 2011 at 2:47 AM, Terry Reedy wrote: > What you are saying is a) that the following code > > for title in ['amazinG', 'a helL of a fiGHT', '', 'igNordEd']: > =A0 =A0print(fix_title(title)) > At least in Python 3.2, this isn't the case. StopIteration breaks the loop only if it's raised during the assignment, not during the body. >>> x=3Diter([1,2,3,4,5]) >>> for i in x: print("%d - %d"%(i,next(x))) 1 - 2 3 - 4 Traceback (most recent call last): File "", line 2, in print("%d - %d"%(i,next(x))) StopIteration Compare with: >>> def pair(it): while True: yield next(it),next(it) >>> x=3Diter([1,2,3,4,5]) >>> for i,j in pair(x): print("%d - %d"%(i,j)) 1 - 2 3 - 4 In this case, the StopIteration bubbles up and quietly terminates the loop. ChrisA