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

Steven D'Aprano steve+comp.lang.python at pearwood.info
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 bad...do you have a link to prior discussion on this and what
> the reasoning was for rejection? 

http://osdir.com/ml/python.python-3000.devel/2006-05/msg00686.html
http://mail.python.org/pipermail/python-list/2001-August/094909.html

E.g.:

if x:
    pass


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.

http://blog.golang.org/2011/01/go-slices-usage-and-internals.html


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


-- 
Steven



More information about the Python-list mailing list