[Python-ideas] Reducing colon uses to increase readability

Ron Adam rrr at ronadam.com
Mon Jun 30 17:59:33 CEST 2008



Terry Reedy wrote:

> I agree that slices could have used something else, but....


Well there is always the slice object.

     slice(start, stop, step)

Maybe if slice was more interchangeable with range or xrange, or if range 
objects could be used in place of slice objects?  <shrug>



A few results form Python 2.5:

 >>> s = slice(10, 20, 3)

 >>> range(s)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got slice.

 >>> range(100)[s]
[10, 13, 16, 19]

 >>> xrange(s)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: an integer is required

 >>> xrange(100)[s]
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'slice'

 >>> list(xrange(100))[s]
[10, 13, 16, 19]







More information about the Python-ideas mailing list