PEP 276 Simple Iterator for ints (fwd)

James_Althoff at i2.com James_Althoff at i2.com
Thu Nov 15 00:04:56 CET 2001


Magnus Lie Hetland wrote:
>IMO it's really a pity that we don't have this sort of thing in Python,
>including possibly a version without the brackets (for iterators,
>perhaps? Or I guess tuples would be more natural, although less suited
>for for loops):
>
>  for i in 1 .. 10: print i
>
>or
>
>  for i in 0, 2 .. 20: print i
>
>But I guess getting this in as part of the language isn't very realistic.

Which of the below would "0 .. 5" specify?
1) 0,1,2,3,4,5
2) 1,2,3,4
3) 0,1,2,3,4
4) 1,2,3,4,5

If not 3), then there is the issue of having to do an extra computation for
the common case of indexing a sequence as in, e.g.,

    for i in 0 .. len(sequence)-1:

OTOH, would it be transparent for "0 .. 5" to mean "0,1,2,3,4"?

In any event, PEP 276 doesn't preclude one from devising such an addition
to Python (PEP 276 is *not* a proposal for a competing, alternative new
syntax for integer sequences).

Indeed, if a new syntax allowed one to create an integer iterator as in:

    for i in 0 .. table.getRowCount():  # assumes open interval on the
right

why not make it even easier for the most common case (since using 0 as the
starting index is such a strong convention) and leave out the "0 .. " by
default to create an integer iterator as in:

    for i in table.getRowCount():  # a la PEP 276!  :-)

(Remember, the real bad guy is xrange ;-)

Jim





More information about the Python-list mailing list