Nested iteration?

2013-04-23 Thread Roy Smith
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):

Re: Nested iteration?

2013-04-23 Thread Oscar Benjamin
On 23 April 2013 16:40, Roy Smith r...@panix.com 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

Re: Nested iteration?

2013-04-23 Thread Ian Kelly
On Tue, Apr 23, 2013 at 9:40 AM, Roy Smith r...@panix.com 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()

Re: Nested iteration?

2013-04-23 Thread Peter Otten
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,

Re: Nested iteration?

2013-04-23 Thread Chris Angelico
On Wed, Apr 24, 2013 at 1:40 AM, Roy Smith r...@panix.com 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()

Re: Nested iteration?

2013-04-23 Thread Ian Kelly
On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico ros...@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

Re: Nested iteration?

2013-04-23 Thread Steven D'Aprano
On Tue, 23 Apr 2013 11:40:31 -0400, 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

Re: Nested iteration?

2013-04-23 Thread Ian Kelly
On Tue, Apr 23, 2013 at 10:30 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico ros...@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).

Re: Nested iteration?

2013-04-23 Thread Chris Angelico
On Wed, Apr 24, 2013 at 2:30 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico ros...@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).

Re: Nested iteration?

2013-04-23 Thread Steven D'Aprano
On Wed, 24 Apr 2013 02:42:41 +1000, Chris Angelico wrote: I love this list. If I make a mistake, it's sure to be caught by someone else. No it's not! Are-you-here-for-the-five-minute-argument-or-the-full-ten-minutes-ly y'rs, -- Steven --

Re: Nested iteration?

2013-04-23 Thread Terry Jan Reedy
On 4/23/2013 11:40 AM, 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:

Re: Nested iteration?

2013-04-23 Thread Joshua Landau
On 23 April 2013 21:49, Terry Jan Reedy tjre...@udel.edu wrote: ri= iter(range(3)) for i in ri: for j in ri: print(i,j) # this is somewhat deceptive as the outer loop executes just once 0 1 0 2 I personally would add a 'break' after 'outer_line = next(f)', since the first

Re: Nested iteration?

2013-04-23 Thread Oscar Benjamin
On 23 April 2013 17:30, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Apr 23, 2013 at 10:21 AM, Chris Angelico ros...@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

Re: Nested iteration?

2013-04-23 Thread Joshua Landau
On 23 April 2013 22:29, Oscar Benjamin oscar.j.benja...@gmail.com wrote: 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 That creates a new list

Re: Nested iteration?

2013-04-23 Thread Oscar Benjamin
On 23 April 2013 22:41, Joshua Landau joshua.landau...@gmail.com wrote: On 23 April 2013 22:29, Oscar Benjamin oscar.j.benja...@gmail.com wrote: 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