Long integers, xrange and number theory

Chad Netzer cnetzer at mail.arc.nasa.gov
Mon Dec 9 17:52:41 EST 2002


On Sunday 08 December 2002 16:51, Delaney, Timothy wrote:

> Just a quick implementation ... we can do better ;)

> def xrange (start, stop=None, step=None, xrange=xrange):
[ snip ]
>     try:
>         return xrange(start, stop, step)

The problem is that people object to replacing the relatively fast 
xrange object (implemented in C, and using C ints if I remember 
correctly), with a slower python object, since the most used case 
with xrange is for python ints, not python longs ints.

So, the idea is that we need to fix range() to allow long ranges 
and return lists of longs when appropriate, and we need to 
implement a new xrange()-like object specifically for long ints 
(lets say it is a generator).  Then we need to change the python 
code to return the normal xrange proxy object (which is NOT a 
generator) when no long integers are involved, and the new 
generator object when they are (this last part is tricky, because 
it amounts to special casing the parsing of xrange()).

That would be the most backwards compatible, with good speed for 
the common case (but harder to implement).

I've been meaning to try doing it, since I've now found two bugs in 
seperate pieces of code (not mine :) )that resulted from the 
assumption that range and xrange could work with long integers.  
Bot resulted from using multiple arguments of (1 / <very small but 
reasonable float>) to the range function (ie. the actual range 
should be of short length, but the values in them wanted to be huge)

Maybe I'll start work on it tonight.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
cnetzer at mail.arc.nasa.gov




More information about the Python-list mailing list