Problem with Python xrange

Robert Brewer fumanchu at amor.org
Sun Jun 6 16:03:58 EDT 2004


Christian Neumann wrote:

> I use Python 2.3.4 (final) and i think there is a bug
> in the built-in function xrange().
>  
> An example is:
>  
> x = xrange(2, 11, 2)  ## [2, 4, 6, 8, 10]
>  
> I get an TypeError if i use it with SliceType:
> 
> x[1:4]  ## It should be return an xrange object with length 3
>  
> Here is the error message: 
>  
> "TypeError: sequence index must be integer".

Hm. I never noticed this either:

>>> x = xrange(2, 11, 2)
>>> x
xrange(2, 12, 2)

But back to your question. Anytime you use the word "should" in a bug
report, you might guess it's really a feature request, not a bug. ;)
Simply put, the "xrange type" doesn't support slicing like that. From
the docs: http://docs.python.org/lib/typesseq.html

"Xrange objects are similar to buffers in that there is no specific
syntax to create them, but they are created using the xrange() function.
They don't support slicing, concatenation or repetition, and using in,
not in, min() or max() on them is inefficient."


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list