Python 3: range objects cannot be sliced

F

Fuzzyman

Is the behavior below expected? Documented?
(The error msg is misleading.)
Thanks,
Alan Isaac

 >>> x = range(20)
 >>> s = slice(None,None,2)
 >>> x
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'slice'


Well, it has the same behaviour as the iterator returned by xrange in
Python 2.X - so expected I guess. The error message is also the same
in Python 2.X.

Michael Foord
 
A

Alan G Isaac

range is an iterator now. Try itertools.islice.

Well yes, it behaves like xrange did.
But (also like xrange) it supports indexing. (!)
So why not slicing?
I expected this (to keep it functionally
more similar to the old range).

Alan Isaac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top