Suggested feature: slice syntax within tuples (or even more generally)?

Steven D'Aprano steve+comp.lang.python at
Thu Feb 14 09:03:50 CET 2013

On Wed, 13 Feb 2013 21:54:43 -0800, stephenwlin wrote:

>> I believe the idea of slice literals has been rejected.
> That's too you have a link to prior discussion on this and what
> the reasoning was for rejection?


if x:

Is that intended as "if slice(x, None, None)" with a missing colon, or 
"if x" with colon supplied?

With the addition of one extra letter, you can use slice notation to 
return slice objects:

class SlicerAndDicer:
    def __getitem__(self, item):
        return item

s = SlicerAndDicer()

And some examples:

py> s[2::5]
slice(2, None, 5)
py> s[::-1]
slice(None, None, -1)
py> s[3, 4]
(3, 4)
py> s[3, 4:6]
(3, slice(4, 6, None))
py> s[7::, 9]
(slice(7, None, None), 9)

I feel all giddy inside...

By the way, Go-lang also has slices, but they're more like views into an 
array than Python's slice objects.

This is not germane to your question, I just found it interesting reading.


More information about the Python-list mailing list