Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13189
| References | <j4gea4$m3b$1@dough.gmane.org> <CALwzidkeRA69p+Vw80xnE_sndx64zr2yXQsFZhS520KYbjxTxA@mail.gmail.com> <j4iomc$te4$1@dough.gmane.org> <CAPTjJmrXNjoccWAjwPO7yGHjQsruvJ7Kn6WWum6ywwWd+GZpoQ@mail.gmail.com> <j4jknm$cpd$1@dough.gmane.org> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-09-12 10:55 -0600 |
| Subject | Re: Idioms combining 'next(items)' and 'for item in items:' |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1040.1315846863.27778.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idioms combining 'next(items)' and 'for item in items:' Ian Kelly <ian.g.kelly@gmail.com> - 2011-09-12 10:55 -0600
csiph-web