PEP 276 Simple Iterator for ints (fwd)

Peter Hansen peter at engcorp.com
Wed Nov 28 00:30:50 EST 2001


Rainer Deyke wrote:
> 
> "Peter Hansen" <peter at engcorp.com> wrote:
> > Greg Ewing wrote:
> > >
> > > While trying to think of a range syntax that looks
> > > unambiguously half-open without clashing with list
> > > or tuple constructors, the following blindingly
> > > obvious solution occurred to me:
> > >
> > >   for 0 <= i < 5:
> > >     ...
> >
> > -1 for being ambiguous to newbies.  
> 
> The same could be said for 'for i in range(5)', since 'i in range(5)' is an
> expression that is roughly equivalent to '0 <= i < 5'.

But range() can be looked up.  You can type range(5) 
at the interactive prompt (where a newbie would live)
and see [0, 1, 2, 3, 4] and infer immediately that
you are stepping through the items one at a time.
Typing 0 <= i < 5 at the prompt gives you either
0 or 1, or NameError: name 'i' is not defined.
Not helpful for a newbie.

(Note: I'm not exactly interested in dumbing a language
down to the point where anyone can understand it 
without learning something from a tutorial or the
reference.  I am uninterested, however, in adding
line noise and moving closer to Perl just because
it "looks" nicer to some.  range() is clean.

> Having written several programs where the vast majority of 'for' loops were
> in the form 'for something in range(something_else)', I would appreciate the
> syntax sugar of 'for i in 5'.  All the other suggestions strike me as worse
> than 'range'.

Definitely an improvement over the above, and almost
elegant in its simplicity.  The fact it means 5
does different things depending on where you type
it is a little concerning.  My argument about
the newbie at the prompt above still applies.

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



More information about the Python-list mailing list