Why can't slice use non-int. indices?

Thomas Wouters thomas at xs4all.net
Fri Jun 1 16:00:40 CEST 2001


On Fri, Jun 01, 2001 at 01:50:14PM +0100, Michael Hudson wrote:
> "Ben Wolfson" <wolfson at uchicago.edu> writes:
> 
> > In article <3B168CF7.F44777F9 at yahoo.com>, "Paul Winkler"
> > <slinkp23 at yahoo.com> wrote:
> > 
> > > This is on python 2.1.
> > > 
> > >>>> a = []
> > >>>> a[3:4]
> > > []
> > >>>> a[3.2: 4]
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in ?
> > > TypeError: slice indices must be integers
> > 
> > For that matter, why doesn't this work?

> Because the sq_item & sq_slice methods in the PySequenceMethod struct
> in object.h is declared as taking ints rather than PyObject*s.  This
> is probably wrong, but it's hard to see how to fix it without breaking
> backwards compatibility.

Note that we *started* doing this in 2.0. Since 2.0, if an object defines
__getitem__ but not __getslice__, all slice operations create a slice object
that gets passed to __getitem__. Unfortunately, because of the way classes
are implemented, we can't get rid of the are-slice-arguments-really-ints
check for class instances. It should be gone for extention classes, but
since very few extention classes implement a sq_getitem that handles slice
objects, it's hard to tell.

Now that we have a warning system, we should probably start warning against
the use of __getslice__ to handle slices...

Let-me-add-it-to-the-end-of-my-TODO-list-ly yrs ;-P

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list