PEP 276 Simple Iterator for ints (fwd)

Peter Hansen peter at engcorp.com
Wed Nov 28 20:39:52 EST 2001


David Eppstein wrote:
> 
>  Peter Hansen <peter at engcorp.com> wrote:
> >
> > 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.

Yes!

> 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.

I'm continually amazed at how easy it is to understand
someone else's point of view when after enough discussion
all the mistaken assumptions are resolved and shared
semantics are established. :-)

I suddenly understand the reasoning behind the suggestion,
and the claimed elegance.  In that light, it's quite nice.

My only real objection now is that I believe my understanding
of what "in" means with "for" is actually the more appropriate, 
and this suggestion does not fit with that view.  Not to 
mention how long it took me to get it: it's not self-evident,
at least to some people who share my starting point.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list