Python: short range xrange


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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 31 other followers

%d bloggers like this: