Seek support for new slice syntax PEP.

Anh Hai Trinh anh.hai.trinh at gmail.com
Tue Dec 15 07:42:12 EST 2009


>         > from numpy import s_
>         > s_[1:2:3]
>         slice(1, 2, 3)
>         > s_[1:2:3, ..., 4:5]
>         (slice(1, 2, 3), Ellipsis, slice(4, 5, None))
>
> Or would it be possible to define "slice" itself so that it implements
> __getitem__ and __getslice__?


Indeed!

Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> class slice(object):
...  @staticmethod
...  def __getitem__(sliceobj):
...    return sliceobj

>>> slice = slice()

>>> slice[:]
slice(None, None, None)

>>> slice[1::-1]
slice(1, None, -1)

>>> range(10).__getitem__(slice[::2])
[0, 2, 4, 6, 8]


----aht



More information about the Python-list mailing list