Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '*not*': 0.07; 'lines,': 0.07; 'iterate': 0.09; 'propagate': 0.09; 'raises': 0.09; '*other*': 0.16; '23,': 0.16; '24,': 0.16; 'ah,': 0.16; 'caveat': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'ignoring': 0.16; 'loop.': 0.16; 'looping': 0.16; 'loops': 0.16; 'pairs': 0.16; 'pairs,': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'stack': 0.19; 'instance,': 0.24; 'file.': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'record': 0.27; 'chris': 0.29; 'am,': 0.29; 'reaches': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'bad.': 0.31; 'catching': 0.31; 'checking.': 0.31; 'exceptions': 0.31; 'raised': 0.31; 'terminate': 0.31; 'stuff': 0.32; 'run': 0.32; '(i.e.': 0.33; 'guess': 0.33; 'there,': 0.34; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'returning': 0.36; 'done': 0.36; 'method': 0.36; 'thanks': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'list.': 0.37; 'received:209': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'even': 0.60; 'catch': 0.60; 'ian': 0.60; 'break': 0.61; 'mentioned': 0.61; 'simply': 0.61; 'simple': 0.61; 'love': 0.65; '(that': 0.65; 'body.': 0.68; 'unusual': 0.74; 'guaranteed': 0.75; '"simply': 0.84; '2:30': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=VaA7PMdNyH+19wagKU94BZR1mKQLQ1zcpFNOzdCY2Pg=; b=WdFcNmBJzUmoowTi3L0qKWzw7lGml5x20QvoNxumTJ+9xxxLT1kqqts5GZsbASsFtJ H3eBj2huIIVFALpooqGcKWVyTTu9S/uapGY54nQJB5mbjJqWxo2rjiYY0Po1CH01oflo X6AO+Hq8LktS+NZ52qCd34C5ocLYtS9FVIk8bKISoEi/ldavRiFYjXk9+5BalZ7TnHdT 5dUj48ctAYmiC1TCk4SOJQEUjsMyVVUQEe9vWaq2ImxCf0c8v+iVq5hiUVXsYYl+4H1L pYZX+PY83XJmzfpSzMTyJHOWeh2399tu5ouoOulYQUK17QAmhgk/8/wWjUBJ1dZ0NDtw hnWQ== MIME-Version: 1.0 X-Received: by 10.52.37.109 with SMTP id x13mr19924172vdj.10.1366735361988; Tue, 23 Apr 2013 09:42:41 -0700 (PDT) In-Reply-To: References: Date: Wed, 24 Apr 2013 02:42:41 +1000 Subject: Re: Nested iteration? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366735371 news.xs4all.nl 2275 [2001:888:2000:d::a6]:49054 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44201 On Wed, Apr 24, 2013 at 2:30 AM, Ian Kelly wrote: > On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico 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). Ah, whoops, my bad. This is what I get for not checking. I know I've done weird stuff with for loops before, but I guess it was fiddling inside the top of it, not in its body. I love this list. If I make a mistake, it's sure to be caught by someone else. The record is guaranteed to be set straight. Thanks Ian! ChrisA