range Failure?

Sean 'Shaleh' Perry shalehperry at attbi.com
Sat Mar 9 17:50:55 EST 2002


On 09-Mar-2002 Colin J. Williams wrote:
> <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
> <html>
> It would seem that the first execution below should deliver the same
> <br>result as the second.
> <br>>>> range(5, 2)
> <br>[]
> <br>>>> range(0, 5, 2)
> <br>[0, 2, 4]

GAH! kill the html please.

range(5,2) says "start at 5 and go to 2" which makes no sense and thus returns
an empty list.

from the python source:

        if (PyTuple_Size(args) <= 1) {
                if (!PyArg_ParseTuple(args,
                                "l;range() requires 1-3 int arguments",
                                &ihigh))
                        return NULL;
        }
        else {
                if (!PyArg_ParseTuple(args,
                                "ll|l;range() requires 1-3 int arguments",
                                &ilow, &ihigh, &istep))
                        return NULL;
        }

Look at the last arguments to each funtion.  If one argument is passed it
because the high value (endpoint).  Otherwise the values are assigned in order
to low, high and then step (which defaults to 1).




More information about the Python-list mailing list