Python: short range xrange

2008-08-26

Pathetic Python:

>>> range(1e10,1e10)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: range() integer start argument expected, got float.
>>> xrange(1e10,1e10)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to int

In the first case range‘s error message is misleading. range is quite happy to take float arguments that are integers:

>>> range(1e1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I suspect that range‘s actual problem is what xrange is complaining about a little lower down: it can’t squeeze 1e10 into an int. It should say so.

xrange however should not be barfing on this input. Well, for one thing the resulting sequence has no elements in it. But even xrange(1e300) is reasonable and should give a reasonable result. There’s no reason that xrange should be constrained by being implemented with int (which in Python is bounded) rather than long (or bignum, as we would say).

Browsing the bug tracker, looks like it might be being fixed.

Leave a comment