Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python 3: range objects cannot be sliced

867 views
Skip to first unread message

Alan G Isaac

unread,
Jan 16, 2009, 12:45:21 PM1/16/09
to
Is the behavior below expected? Documented?
(The error msg is misleading.)
Thanks,
Alan Isaac

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

Fuzzyman

unread,
Jan 16, 2009, 1:02:26 PM1/16/09
to

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
--
http://www.ironpythoninaction.com/

Paul Rubin

unread,
Jan 16, 2009, 1:15:42 PM1/16/09
to
Alan G Isaac <alan....@gmail.com> writes:
> >>> x = range(20)
> >>> s = slice(None,None,2)
> >>> x[s]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: sequence index must be integer, not 'slice'

range is an iterator now. Try itertools.islice.

Alan G Isaac

unread,
Jan 16, 2009, 1:36:34 PM1/16/09
to
On 1/16/2009 1:15 PM Paul Rubin apparently wrote:
> 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

Christian Heimes

unread,
Jan 16, 2009, 1:57:47 PM1/16/09
to pytho...@python.org
Alan G Isaac schrieb:

The old range function returned a list. If you need the old
functionality you can use list(range(...))[].

Why do you want to slice a range anyway? The range type supports a
start, stop and step argument.

Christian

Alan G Isaac

unread,
Jan 16, 2009, 3:13:00 PM1/16/09
to
0 new messages