PEP 276 Simple Iterator for ints (fwd)
David Eppstein
eppstein at ics.uci.edu
Wed Nov 28 14:44:05 EST 2001
In article <3C048CCC.6A8C3743 at engcorp.com>,
Peter Hansen <peter at engcorp.com> wrote:
> I agree that sounds like a worthy goal. I disagree that your
> suggestion is any more likely to be understood by anyone,
> other than perhaps a mathematician. As a very experienced
> programmer, I found it quite obscure and am unsure I
> would have figured it out were it not for the context of
> recent threads on the subject.
>
> Maybe we should look back to BASIC (for i = 1 to 5: next)
> or one of the suggestions for something which looks more
> explicitly like a list of items (for i in [0..5]) or
> (for i in [0, 1, .. 5] ). Especially this latter would
> definitely be understandable to anyone, IMHO.
After more thought, I am coming to like Ewing's suggestion of
for 0 <= i < 5:
much better than the earlier-discussed
for i in [0, 1, .. 4]:
The latter notation now seems unnecessarily redundant. Think of it by
analogy -- which of the following would you most likely use,
if 0 <= i < 5:
or
if i in [0, 1, .. 4]:
or
if i in range(5):
or even (with Perl-like conciseness)
if i in 5:
?
Another advantage of Ewing's suggestion is that it cleanly avoids the whole
closed versus half-open debate.
The biggest disadvantage of Ewing's suggestion is the lack of a stepsize
(other than -1 which is easily handled by reversing the inequalities). But
I think +1 and -1 are by far the most common steps, and anything else can
be handled by multiplying the index within the loop, or by using range().
--
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