[Python-Dev] Ranges

Ka-Ping Yee Ka-Ping Yee <pingster@ilm.com>
Wed, 12 Jul 2000 13:22:07 -0700 (PDT)


On Wed, 12 Jul 2000, Paul Prescod wrote:
> Python 2.0b1 (#12, Jul  1 2000, 13:14:33)
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
> >>> [0,1,2,3,4,5,6,7,8,9,10][0:5:2]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: sequence index must be integer

Yeah.  At the moment, [:] calls __getslice__ but [::] calls __getitem__
(ick).  The list object is getting a call to __getitem__(slice(0, 5, 2))
and __getitem__ wants an integer.  I was assuming this would get cleaned
up along with the introduction of [x:y:z]-style range-makers (i recall
Guido posting a suggestion for how to do so a little while back).

Ah, yes: http://www.python.org/pipermail/python-dev/2000-July/012681.html

The issues were

    - handling [:] and [::] consistently
    - allowing non-integer indices
    - letting user-defined objects do their own handling of negative indices

I believe the two possibilities were

    __getitem__ gets either an index or a special slice object
    __getslice__ is deprecated (when missing, __getitem__ gets the slice args)

and

    __getitem__ always gets an index
    __getslice__ gets three arguments, all optional, defaulting to None

The latter isn't backward-compatible, but it makes a heckuva lot
more sense.



-- ?!ng