Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13151

Re: Idioms combining 'next(items)' and 'for item in items:'

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 <rosuav@gmail.com>
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 <j4iomc$te4$1@dough.gmane.org>
References <j4gea4$m3b$1@dough.gmane.org> <CALwzidkeRA69p+Vw80xnE_sndx64zr2yXQsFZhS520KYbjxTxA@mail.gmail.com> <j4iomc$te4$1@dough.gmane.org>
Date Mon, 12 Sep 2011 08:41:05 +1000
Subject Re: Idioms combining 'next(items)' and 'for item in items:'
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1009.1315780869.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Sep 12, 2011 at 2:47 AM, Terry Reedy <tjreedy@udel.edu> wrote:
> What you are saying is a) that the following code
>
> for title in ['amazinG', 'a helL of a fiGHT', '', 'igNordEd']:
>    print(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=iter([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 "<pyshell#281>", line 2, in <module>
    print("%d - %d"%(i,next(x)))
StopIteration

Compare with:

>>> def pair(it):
	while True:
		yield next(it),next(it)

>>> x=iter([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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Idioms combining 'next(items)' and 'for item in items:' Chris Angelico <rosuav@gmail.com> - 2011-09-12 08:41 +1000

csiph-web