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


Groups > comp.lang.python > #13189 > unrolled thread

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

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2011-09-12 10:55 -0600
Last post2011-09-12 10:55 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Idioms combining 'next(items)' and 'for item in items:' Ian Kelly <ian.g.kelly@gmail.com> - 2011-09-12 10:55 -0600

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

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-09-12 10:55 -0600
SubjectRe: Idioms combining 'next(items)' and 'for item in items:'
Message-ID<mailman.1040.1315846863.27778.python-list@python.org>
On Sun, Sep 11, 2011 at 6:45 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> 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.

But you can't write the function under the assumption that it will
only be called from the function body.  The following is a slight
reorganization of your example that does exhibit the problem:

for title in map(fix_title, ['amazinG', 'a helL of a fiGHT', '', 'igNordEd']):
    print(title)

Output:
amazing
a Hell of a Fight

Note that at first glance, my example would appear to be functionally
equivalent to yours -- I've merely pulled the fix_title call out of
the loop body and into the iterator.  But actually they produce
different results because fix_title misbehaves by not catching the
StopIteration.

Cheers,
Ian

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web