Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'nested': 0.07; 'worked.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; "wouldn't": 0.14; 'posted': 0.15; 'iterable': 0.16; 'iterator': 0.16; 'reason.': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'catching': 0.31; 'though.': 0.31; 'another': 0.32; '(i.e.': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'subject:?': 0.36; 'received:209': 0.37; 'somebody': 0.38; 'rather': 0.38; 'bad': 0.39; 'new': 0.61; 'today,': 0.61; 'skip:n 10': 0.64; 'smith': 0.68; 'legal': 0.71; 'guaranteed': 0.75; "else's": 0.84; 'oscar': 0.84; '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=AsJIn63HDg83fYoO30qg6qaBWiMj6GxT60tz4f8LoZg=; b=eWZZoQVkzEcvehaqApjVvVWkSLkipZmD8XebSL2cCorYtFAW4Cytoqn7acSfUfAB5/ S802JyS6//RRjgC+fLVH3VjrsFwF2GhUHFJnxtSL2GCma43IqVE9CnDKo/6DVQkbUcMs X7nYAGZj3W1WU7oIJo/acOSCQJqDadDcixs9DkrNwa6BpUmRoJMGdbP4GYB35EIzEtMk w4Q7ydnc1sKwuKHWlLe4tTTrALj4ZO/p/Yf/PSS+MJwKd7fZ10t6fhRYG+/ssvj26qvC RXRougMMeVEIVR0hg/NtkjPBijF6zgqwKC7WiFOTTzZAHbLilRbLISvNLotcUYd2H8eS 7tzg== X-Received: by 10.58.220.129 with SMTP id pw1mr23056526vec.32.1366733134908; Tue, 23 Apr 2013 09:05:34 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Tue, 23 Apr 2013 17:05:13 +0100 Subject: Re: Nested iteration? To: Roy Smith Content-Type: text/plain; charset=ISO-8859-1 Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366733143 news.xs4all.nl 2168 [2001:888:2000:d::a6]:55274 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44193 On 23 April 2013 16:40, Roy Smith wrote: > In reviewing somebody else's code today, I found the following > construct (eliding some details): > > f = open(filename) > for line in f: > if re.search(pattern1, line): > outer_line = f.next() > for inner_line in f: > if re.search(pattern2, inner_line): > inner_line = f.next() > > Somewhat to my surprise, the code worked. I didn't know it was legal > to do nested iterations over the same iterable (not to mention mixing > calls to next() with for-loops). Is this guaranteed to work in all > situations? For Python 3 you'd need next(f) instead of f.next(). Otherwise, yes, this works just fine with any non-restarting iterator (i.e. so that __iter__ just returns self rather than a new iterator). I recently posted in another thread about why it's a bad idea to call next() without catching StopIteration though. I wouldn't accept the code above for that reason. Oscar