Range() limit

Magnus Lie Hetland mlh at furu.idi.ntnu.no
Fri Feb 21 22:45:01 EST 2003


In article <3E56D3FD.85281214 at alcyone.com>, Erik Max Francis wrote:
>Marc wrote:
>
[snip]
>
>range _actually builds a list_.  If the number of elements in that list
>is huge, you're going to be building a really big list.  If you're using
>it solely for the purposes of iterating over, obviously this ain't the
>brightest idea in the world.  Use xrange instead,

That's good advice, of course, but won't really help. Once the limit
exceets the integer range (and, thus, is a long) xrange will no longer
work (for some reason I don't really understand -- why must xrange use
ints?)

>>> xrange(10**10)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to int
>>>

(This has actually burned me while running some benchmarks -- I ended
up writing my own xrange-like generator that used longs.)

> or simply count manually.

That is (as far as I know) the only way, currently, to have counting
loops with arbitrarily large upper limits.

-- 
Magnus Lie Hetland               "Nothing shocks me. I'm a scientist." 
http://hetland.org                                   -- Indiana Jones




More information about the Python-list mailing list