PEP 276 Simple Iterator for ints (fwd)

David Eppstein eppstein at ics.uci.edu
Wed Nov 28 19:05:37 EST 2001


In article <3C05758F.DBD8833 at engcorp.com>,
 Peter Hansen <peter at engcorp.com> wrote:

> > You are missing the point.  Typing 'i in range(5)' gives the same result at
> > the interactive prompt.  So does 'i in [0, 1, 2, 3, 4]'.
> 
> I don't understand the point.  I wouldn't have expected
> anyone to type "i in range(5)" at the prompt, but simply
> "range(5)", resulting in a list, from which the hypothetical
> newbie would likely infer that the for statement was iterating
> over the items in that list.

I take it you are viewing the syntax of a for-loop as
  "for" variable "in" expression:
where "for" and "in" are keywords and expression must be something that 
returns a list or iterator.  So the "in" of a for-loop has very little 
other than spelling to do with "in" operator for list expressions.

I would rather view a for-loop as
  "for" expression:
where (currently) the expression must have as its top level operation the 
"in" operation and the lhs must be a variable.  The variable is set in all 
possible ways that make the expression true.  From this point of view, "for 
x in 5" looks really ugly, and "for 0 <= x < 5" makes a lot more sense: we 
now simply allow one more kind of expression in a for loop, one where the 
top level is a three-way comparison and the middle side of the comparison 
must be a variable.
-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list