[Tutor] xrange() with start or stop > sys.maxint?

spawgi at gmail.com spawgi at gmail.com
Thu May 19 08:35:09 CEST 2011


What is the version of python you are using?

>From the documentation of python 2.71.
http://docs.python.org/library/functions.html#xrange

CPython implementation detail: xrange() is intended to be simple and fast.
Implementations may impose restrictions to achieve this. The C
implementation of Python restricts all arguments to native C longs (“short”
Python integers), and also requires that the number of elements fit in a
native C long. If a larger range is needed, an alternate version can be
crafted using the itertools module: islice(count(start, step),
(stop-start+step-1)//step).

Hope this helps.

Thanks and Regards,
Sumod


On Thu, May 19, 2011 at 6:47 AM, Terry Carroll <carroll at tjc.com> wrote:

> Is there any way to use xrange with a start or stop value that exceeds
> sys.maxint?
>
>  import sys
>>>> print sys.maxint
>>>>
>>> 2147483647
>
>> start = sys.maxint-1
>>>> for i in xrange(start, start+1):
>>>>
>>> ...   pass
> ...
>
>> start = sys.maxint
>>>> for i in xrange(start, start+1):
>>>>
>>> ...   pass
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> OverflowError: Python int too large to convert to C long
>
>>
>>>>
> Works okay with range, though:
>
>  start = sys.maxint
>>>> for i in range(start, start+1):
>>>>
>>> ...   pass
> ...
>
>>
>>>>  _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110519/9341a7ef/attachment.html>


More information about the Tutor mailing list