Striding slice?

Alex Martelli aleax at aleax.it
Mon Aug 27 10:13:11 CEST 2001


"Brian Lee" <senux at senux.com.NOSPAM> wrote in message
news:3B89F39B.4050103 at senux.com.NOSPAM...
> Bengt Richter wrote:
> >  >>> a='0123456789'
> >  >>> a[0:5]
> >  '01234'
> >  >>> a[0:5:2]
> >  Traceback (most recent call last):
> >    File "<stdin>", line 1, in ?
> >  TypeError: sequence index must be integer
> >  >>>
> >
> > How about (faked example):
> >  >>> a[0:5:2]
> >  '024'
>
> >>> a = '0123456789'
> >>> a[0:5]
> '01234'
> >>> a[0:5][:2]
> '01'

But that's got nothing to do with stride!  The issue
is that strings don't support slices as indices via
getitem (as is now recommended practice), but rather
the older getslice special method (now obsolescent
and not recommended).  It would be nice if strings,
Unicode strings, lists, tuples, array.array's, &c, all
supported slices via getitem... that would afford
the stride aspect of slices to work on them -- so far
it only works on Numeric.array or user-coded classes
and types that respect the new convention (but NOT
ones that inherit a getslice method, sigh:-).


Alex






More information about the Python-list mailing list