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


Groups > comp.lang.python > #13152

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Idioms combining 'next(items)' and 'for item in items:'
Date 2011-09-11 20:45 -0400
References <j4gea4$m3b$1@dough.gmane.org> <CALwzidkeRA69p+Vw80xnE_sndx64zr2yXQsFZhS520KYbjxTxA@mail.gmail.com> <j4iomc$te4$1@dough.gmane.org> <CAPTjJmrXNjoccWAjwPO7yGHjQsruvJ7Kn6WWum6ywwWd+GZpoQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1010.1315788359.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 9/11/2011 6:41 PM, Chris Angelico wrote:
> 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.

It breaks the loop *silently* only if ...

>>>> 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

whereas, you are right, it breaks it noisily in the body. So Ian's claim 
that StopIteration must be caught to avoid silent termination is not 
true. Thanks for pointing out what I saw but did not cognize the full 
implication of before. A better exception and an error message with an 
explaination might still be a good idea, though.

-- 
Terry Jan Reedy

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


Thread

Re: Idioms combining 'next(items)' and 'for item in items:' Terry Reedy <tjreedy@udel.edu> - 2011-09-11 20:45 -0400

csiph-web