Idioms combining 'next(items)' and 'for item in items:'
ian.g.kelly at gmail.com
Mon Sep 12 12:55:14 EDT 2011
On Sun, Sep 11, 2011 at 6:45 PM, Terry Reedy <tjreedy at 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']):
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
More information about the Python-list