Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Steven D'Aprano
On Wed, 29 Feb 2012 10:24:18 +1100, Chris Angelico wrote: > Every syntactic structure should > have the addition of a "foo:" suite, which will run when the programmer > expects it to and no other time. This would solve a LOT of problems. Indeed, when I design my killer language, the identifie

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Chris Angelico
On Wed, Feb 29, 2012 at 9:56 AM, Rick Johnson wrote: > On Feb 24, 8:54 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: > >> In Python 4000, I think for loops should be spelled: >> >> for name in iterable: >>     # for block >> then: >>     # only if not exited with break >> else: >>  

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Rick Johnson
On Feb 24, 8:54 am, Steven D'Aprano wrote: > for...else is a very useful construct, but the name is misleading. It > took me a long time to stop thinking that the else clause executes when > the for loop was empty. Agreed. This is a major stumbling block for neophytes. > In Python 4000, I think

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Peter Otten
Steven D'Aprano wrote: >> The code in the else suite executes only when the for loop is left via >> break. A non-empty iterable is required but not sufficient. > > You have a typo there. As your examples show, the code in the else suite > executes only when the for loop is NOT left via break (or

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Arnaud Delobelle
On 24 February 2012 14:54, Steven D'Aprano wrote: > for...else is a very useful construct, but the name is misleading. It > took me a long time to stop thinking that the else clause executes when > the for loop was empty. This is why I think we should call this construct "for / break / else" rat

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Steven D'Aprano
On Fri, 24 Feb 2012 13:44:15 +0100, Peter Otten wrote: for i in []: > ... pass > ... else: > ... print "else" > ... > else for i in [42]: > ... pass > ... else: > ... print "else" > ... > else for i in [42]: > ... break > ... else: > ... print "else" > ... >>

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Peter Otten
Peter Otten wrote: > The code in the else suite executes only when the for loop is left via > break. Oops, the following statement is nonsense: > A non-empty iterable is required but not sufficient. Let me try again: A non-empty iterable is required but not sufficient to *skip* the else-suite

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Peter Otten
Rick Johnson wrote: > On Feb 23, 6:30 pm, Alex Willmer wrote: >> [...] >> as a standard looping-with-index construct. In Python for loops don't >> create a scope, so the loop variables are available afterward. I've >> sometimes used this to print or return a record count e.g. >> >> for i, x in en

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Rick Johnson
On Feb 23, 6:30 pm, Alex Willmer wrote: > [...] > as a standard looping-with-index construct. In Python for loops don't > create a scope, so the loop variables are available afterward. I've > sometimes used this to print or return a record count e.g. > > for i, x in enumerate(seq): >    # do stuff

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-24 Thread Peter Otten
Ethan Furman wrote: > Steven D'Aprano wrote: >> On Thu, 23 Feb 2012 16:30:09 -0800, Alex Willmer wrote: >> >>> This week I was slightly surprised by a behaviour that I've not >>> considered before. I've long used >>> >>> for i, x in enumerate(seq): >>># do stuff >>> >>> as a standard looping-

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-23 Thread Mark Lawrence
On 24/02/2012 03:49, Ethan Furman wrote: Steven D'Aprano wrote: On Thu, 23 Feb 2012 16:30:09 -0800, Alex Willmer wrote: This week I was slightly surprised by a behaviour that I've not considered before. I've long used for i, x in enumerate(seq): # do stuff as a standard looping-with-index co

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-23 Thread Ethan Furman
Steven D'Aprano wrote: On Thu, 23 Feb 2012 16:30:09 -0800, Alex Willmer wrote: This week I was slightly surprised by a behaviour that I've not considered before. I've long used for i, x in enumerate(seq): # do stuff as a standard looping-with-index construct. In Python for loops don't crea

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-23 Thread Paul Rubin
Alex Willmer writes: > i = 0 > for x in seq: > # do stuff > i += 1 > print 'Processed %i records' % i > > Just thought it worth mentioning, curious to hear other options/ > improvements/corrections. Stephen gave an alternate patch, but you are right, it is a pitfall that can be easy to mi

Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-23 Thread Steven D'Aprano
On Thu, 23 Feb 2012 16:30:09 -0800, Alex Willmer wrote: > This week I was slightly surprised by a behaviour that I've not > considered before. I've long used > > for i, x in enumerate(seq): ># do stuff > > as a standard looping-with-index construct. In Python for loops don't > create a scope