[Python-3000] Using range()

Nick Coghlan ncoghlan at gmail.com
Thu Apr 24 17:50:22 CEST 2008


Martin v. Löwis wrote:
>>>  > This is a bug, right?
>>>
>>> I'd call it an implementation limitation.
>> This is because I'm in a 32 bit machine?
> 
> Right. The assumption is that you typically use
> the range elements to index into some collections,
> and you can't have collections with more than 2**32
> elements (actually, address space is exhausted at
> 2**29 elements already, except for str and unicode).
> 
> It would be possible to make it support larger
> ranges, but then the common case would get slower,
> and the code would be more convoluted.

There's definitely some bugs in this area of the range object code though:

 >>> x = range(2**33, 2)
 >>> len(x)
0
 >>> x[0]
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
IndexError: range object index out of range

I also believe that the OverflowError from doing len(self) while 
attempting to index into the range should be intercepted and converted 
to something more meaningful for the actual operation requested by the 
programmer (e.g. "ValueError: Cannot index range objects with 
sys.maxsize or more elements")

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list