range or xrange disallowed for big numbers

Alex Martelli aleax at aleax.it
Sat Oct 5 12:23:05 CEST 2002

Chad Netzer wrote:

> Is there a reason (technical or philosophical) to disallow:
>     range( 10000000000L, 10000000000L + 1L )
> or
>     xrange( 10000000000L )
> etc...?

Slowing down common cases of range and xrange (by keeping PyObject's
or Python longs rather than C-level long's) is an unpleasant prospect,
although the generality of looping easily over whatever range one
may want can sometimes be useful.  Maybe rather than fixing either
type a new, perhaps Python-coded built-in generator could be added:

def genrange(start, stop=None, step=1):
    if step == 0:
        raise ValueError, "step can't be 0"
    if stop is None:
        start, stop = 0, start
    if step > 0:
        while start < stop:
            yield start
            start += step
        while start > stop:
            yield start
            start += step


More information about the Python-list mailing list