[Tutor] Workaround for limitation in xrange()?

Andrei project5 at redrival.net
Tue Oct 10 17:39:15 CEST 2006


Dick Moores <rdm <at> rcblue.com> writes:

<snip> 
> I can think of 2 workarounds for this 2**31 limitation in xrange(). 
> Use a while loop instead. Or use 2 or more for loops, keeping the 
> number of cycles in each under 2**31.
> 
> Are there others?

Write your own iterator:

>>> def hugerange(minval, maxval):
...     val = minval
...     while val < maxval:
...         yield val
...         val += 1
...
>>> for i in hugerange(2**31, 2**31 + 2):
...     print i
...
2147483648
2147483649

It might be slower than Python's xrange implementation (haven't tested), but it
will take anything you throw at it as long as Python itself can handle it.

Yours,

Andrei



More information about the Tutor mailing list