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

Christopher A. Craig com-nospam at ccraig.org
Thu May 31 15:39:16 EDT 2001


Paul Winkler <slinkp23 at yahoo.com> writes:

> Am I weird for thinking this sort of thing is an obvious
> application for slice notation?  Or that
> some_instance.slice(end="c") is a burdensome way to have to
> implement this kind of thing?

It's actually less consistant that you think

>>> class t:
...   def __getitem__(self, key):
...      print '__getitem__: ', key
...   def __getslice__(self, start, stop):
...      print '__getslice__: ', start, stop
...
>>> a = t()
>>> a[2:3.2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: slice indices must be integers
>>> a[2:3.2:'s']
__getslice__: slice(2, 3.2000000000000002, 's')


So if you use two colons it will work, but if you only use one it
won't.  I think you should write a PEP.

-- 
Christopher A. Craig <com-nospam at ccraig.org>
"Parity is for farmers." Seymour Cray on his machines lack of parity
"I guess farmers buy a lot of computers." Seymour Cray on including parity



More information about the Python-list mailing list