Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44217
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '*not*': 0.07; 'lines,': 0.07; '[1,': 0.09; 'iterate': 0.09; 'propagate': 0.09; 'raises': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '*other*': 0.16; '23,': 0.16; 'caveat': 0.16; 'happily': 0.16; 'ignoring': 0.16; 'loop.': 0.16; 'looping': 0.16; 'loops': 0.16; 'pairs': 0.16; 'pairs,': 0.16; 'starred': 0.16; 'unpack': 0.16; 'unpacking': 0.16; 'valueerror': 0.16; 'wrote:': 0.18; 'stack': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'convenient': 0.24; 'instance,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'reaches': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '"",': 0.31; 'catching': 0.31; 'exceptions': 0.31; 'raised': 0.31; 'terminate': 0.31; 'file': 0.32; 'run': 0.32; '(i.e.': 0.33; '(most': 0.33; 'there,': 0.34; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'problem.': 0.35; 'definition': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'described': 0.36; 'returning': 0.36; 'method': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'being': 0.38; 'recent': 0.39; 'called': 0.40; 'even': 0.60; 'catch': 0.60; 'ian': 0.60; 'break': 0.61; 'mentioned': 0.61; 'numbers': 0.61; 'simply': 0.61; 'simple': 0.61; 'first': 0.61; 'more': 0.64; '(that': 0.65; 'occur': 0.65; 'to:addr:gmail.com': 0.65; 'unusual': 0.74; '"simply': 0.84; 'oscar': 0.84; 'numbers:': 0.91; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=G0MAhx9Yn8WR3a6iKeilhh09gIB99HcyEY/2h361OrU=; b=ctVXOtn+XGp5FtEsM6X01O+NkSjvam9tAGWS2i0H/blXli+MohoKGx5m5E95QV+jSM x4wnxQSHpKxChMhM4taUCLb/4kPhxpBcq5ckILiZLpeCzzKowqkMw2VcJYHBlEd28Qkm wWrH/xFBBWD4yif1uvsa4y4/ZABErxwQ5C7Wy0LFb9RljgmmmY/fAvonVjQhHNn9RiAV FtNv/G3gndh63lYcEFeJWtb6tfqP48Sbo8EmlG/jv4s+pyTRWO9VGt+UFMYLesvzjikJ wMfx5aCViL+AMDGezSQbCd/wUeAREDPyDo2oP8cLqBmbMd/zmZRT1cd8SI/tPEiEmkbB owOg== |
| X-Received | by 10.58.161.6 with SMTP id xo6mr23544603veb.50.1366752567590; Tue, 23 Apr 2013 14:29:27 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <CALwzidk6qz4qHOFWYqv_cDEtjbhvS_kjG0vXJZmeB0dJ07doTQ@mail.gmail.com> |
| References | <kl6a1f$k2l$1@panix2.panix.com> <CAPTjJmpKR8z7Cub1WaurmsuugNgCvPYFdh9WYqJd_1OTfpLw6Q@mail.gmail.com> <CALwzidk6qz4qHOFWYqv_cDEtjbhvS_kjG0vXJZmeB0dJ07doTQ@mail.gmail.com> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Tue, 23 Apr 2013 22:29:07 +0100 |
| Subject | Re: Nested iteration? |
| To | Ian Kelly <ian.g.kelly@gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | Python <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.996.1366752575.3114.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366752575 news.xs4all.nl 2258 [2001:888:2000:d::a6]:52104 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44217 |
Show key headers only | View raw
On 23 April 2013 17:30, Ian Kelly <ian.g.kelly@gmail.com> wrote: > On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico <rosuav@gmail.com> wrote: >> The definition of the for loop is sufficiently simple that this is >> safe, with the caveat already mentioned (that __iter__ is just >> returning self). And calling next() inside the loop will simply >> terminate the loop if there's nothing there, so I'd not have a problem >> with code like that - for instance, if I wanted to iterate over pairs >> of lines, I'd happily do this: >> >> for line1 in f: >> line2=next(f) >> print(line2) >> print(line1) >> >> That'll happily swap pairs, ignoring any stray line at the end of the >> file. Why bother catching StopIteration just to break? > > The next() there will *not* "simply terminate the loop" if it raises a > StopIteration; for loops do not catch StopIteration exceptions that > are raised from the body of the loop. The StopIteration will continue > to propagate until it is caught or it reaches the sys.excepthook. In > unusual circumstances, it is even possible that it could cause some > *other* loop higher in the stack to break (i.e. if the current code is > being run as a result of the next() method being called by the looping > construct). I don't find that the circumstances are unusual. Pretty much any time one of the functions in the call stack is a generator this problem will occur if StopIteration propagates. I just thought I'd add that Python 3 has a convenient way to avoid this problem with next() which is to use the starred unpacking syntax: >>> numbers = [1, 2, 3, 4] >>> first, *numbers = numbers >>> first 1 >>> for x in numbers: ... print(x) ... 2 3 4 >>> first, *numbers = [] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 0 values to unpack Since we get a ValueError instead of a StopIteration we don't have the described problem. Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Nested iteration? roy@panix.com (Roy Smith) - 2013-04-23 11:40 -0400
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 17:05 +0100
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:05 -0600
Re: Nested iteration? Peter Otten <__peter__@web.de> - 2013-04-23 18:15 +0200
Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:21 +1000
Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:35 +0000
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:30 -0600
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:39 -0600
Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:42 +1000
Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:53 +0000
Re: Nested iteration? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 16:49 -0400
Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:14 +0100
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 22:29 +0100
Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:41 +0100
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 23:42 +0100
csiph-web