PEP 284, Integer for-loops

Quinn Dunkan quinn at cruzeiro.ugcs.caltech.edu
Sat Mar 9 14:56:56 EST 2002


On Wed, 06 Mar 2002 08:41:31 -0800, David Eppstein <eppstein at ics.uci.edu>
wrote:
>Rationale
>
>    One of the most common uses of for-loops in Python is to iterate
>    over an interval of integers.

Not even close, for me.  I usually iterate over sequences (which are
not of consecutive integers).

>                                   Python provides functions range()
>    and xrange() to generate lists and iterators for such intervals,
>    which work best for the most frequent case: half-open intervals
>    increasing from zero.  However, the range() syntax is more awkward
>    for open or closed intervals, and lacks symmetry when reversing
>    the order of iteration.

That's because what you really want in those cases is a different function:

def hrange(x, y):
    if x < y:
        while x != y:
            yield x
            x += 1
    else:
        while x != y:
            yield x
            x -= 1
    yield y

>                             In addition, the call to an unfamiliar
>    function makes it difficult for newcomers to Python to understand
>    code that uses range() or xrange().

Anyone to whom the basic builtins are unfamiliar doesn't know python and can't
be expected to understand it very well <wink>.

>Specification
>
>    We propose to extend the syntax of a for statement, currently

Personally, I wouldn't mind seeing the rate of syntax extensions slow down
a bit.


So I can't really say I'm in favor of this one, sorry :(



More information about the Python-list mailing list