Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13198
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'received:verizon.net': 0.07; 'terry': 0.07; 'context.': 0.09; 'interpreter,': 0.09; 'loop.': 0.09; 'persisting': 0.09; 'problem:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'exception': 0.12; 'meaningful': 0.13; "'',": 0.16; 'clauses.': 0.16; 'expected,': 0.16; 'input.': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'occasionally': 0.16; 'pulled': 0.16; 'reedy': 0.16; 'termination': 0.16; 'valueerror': 0.16; 'wrote:': 0.16; 'defined': 0.19; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'appear': 0.23; 'body.': 0.23; 'breaks': 0.23; 'exhibit': 0.23; 'sep': 0.23; 'though.': 0.23; "user's": 0.23; 'pm,': 0.24; 'testing': 0.24; 'function': 0.27; 'raise': 0.28; 'random': 0.28; 'loop': 0.28; 'right.': 0.28; 'correct': 0.28; 'pass': 0.29; 'explicitly': 0.29; 'fixed': 0.29; '(and': 0.29; 'example': 0.30; 'bare': 0.30; 'important,': 0.30; 'kelly': 0.30; 'sun,': 0.30; 'thanks': 0.30; "skip:' 10": 0.30; 'equivalent': 0.31; 'error': 0.32; 'values': 0.32; 'cases': 0.32; 'pointing': 0.32; 'does': 0.32; 'actually': 0.33; "can't": 0.33; 'there': 0.33; 'to:addr :python-list': 0.33; "i've": 0.34; 'difference': 0.34; 'header :User-Agent:1': 0.34; '(as': 0.34; 'right,': 0.34; 'test': 0.34; 'explicit': 0.34; 'flag': 0.34; 'sometimes,': 0.34; 'subject:next': 0.34; 'header:X-Complaints-To:1': 0.35; 'rather': 0.35; 'thank': 0.35; 'unless': 0.36; 'couple': 0.36; 'before.': 0.37; 'response,': 0.37; 'several': 0.37; 'but': 0.37; 'not,': 0.38; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'under': 0.39; 'header:Mime-Version:1': 0.39; 'user': 0.39; 'meaning': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'called': 0.40; 'might': 0.40; 'more': 0.60; 'your': 0.61; 'body': 0.61; 'stop': 0.61; 'results': 0.61; 'unique': 0.62; 'full': 0.63; 'alternative': 0.64; 'believe': 0.65; 'view': 0.67; 'response.': 0.67; '11,': 0.68; 'amazing': 0.68; 'saw': 0.69; 'legal': 0.70; 'claim': 0.76; 'exclude': 0.77; 'fight': 0.80; 'grabbing': 0.84; 'leak': 0.84; 'yielded': 0.84; 'yours': 0.85 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Idioms combining 'next(items)' and 'for item in items:' |
| Date | Mon, 12 Sep 2011 16:51:01 -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> <j4jknm$cpd$1@dough.gmane.org> <CALwzidnPj4rLpyk3NvogZshQd8y2rJnKrii47-5_N8FNBKf8Hg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-74-109-121-73.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 |
| In-Reply-To | <CALwzidnPj4rLpyk3NvogZshQd8y2rJnKrii47-5_N8FNBKf8Hg@mail.gmail.com> |
| 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.1047.1315860683.27778.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1315860683 news.xs4all.nl 2538 [2001:888:2000:d::a6]:42557 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:13198 |
Show key headers only | View raw
On 9/12/2011 12:55 PM, Ian Kelly wrote: > 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. Sigh. You are right. > 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. You are right, non-iterators should not raise or pass on StopIteration. There are actually several reasons. 1. The manual defined StopIteration as meaning '[I have] no more values [to give you]'. This is only meaningful coming from an iterator. 2. Your particular point is that StopIteration is (almost) unique in being sometimes, but only sometimes, caught by the interpreter, rather than just by user except clauses. AttributeError is another, which has occasionally caused its own problems. But we cannot stop raising AttributeError while we can always catch StopIteration for explicit next() (and should outside of iterators). 3. In the case of grabbing the first item from an iterator, no first item is a boundary case for the expected, legal type of input. I believe boundary cases should be included in function specifications. While there may be a couple of choices as to response, that is much less than infinity. For fix_title, the choices are ValueError or ''. Any other return would be an error unless explicitly given in the specs. So the boundary case should be included in the test suite to exclude any other random response. 4. StopIteration is an artifact of the choice of implementation. Pulling the first item out before the loop is an alternative to a flag and testing within the loop. Such an implementation detail should not leak into the user's view of the function as an abstraction. If fix_title were a generator function whose instances yielded fixed title words one at a time, then the bare next() would be correct (as you noted). But it is not, and the difference is important, more important than having 'minimal clean code'. Thank you for persisting until I saw that in this context. -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idioms combining 'next(items)' and 'for item in items:' Terry Reedy <tjreedy@udel.edu> - 2011-09-12 16:51 -0400
csiph-web