xrange issue 7721

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun May 30 02:55:14 EDT 2010


On Sat, 29 May 2010 19:46:28 +0100, Mark Lawrence wrote:

> I've had an OverflowError using xrange with Python 2.6.5 on Windows.
> Googling got me to the subject line.

It is considered best practice (or at least sensible practice) to include 
a relevant URL in your post. This will maximise the number of people who 
click through to the bug tracker while minimising the number who say "if 
the poster can't be bothered to copy and paste a URL, he obviously 
doesn't care that much about the issue, so why should I google for it?".

http://bugs.python.org/issue7721

 
[...]
> Assuming that I am correct, can I create myself a login on the bugs
> tracker and re-open the issue to get this sorted?

You can try, but I don't think that a code snippet is meant as a full-
blown replacement for xrange, just as a, well, snippet to get you started.

If you need a full replacement, you'll end up with something like this:

_xrange = xrange
def xrange(a, b=None, step=1):
    try:
        return _xrange(a, b, step)
    except OverflowError:
        if b is None:
            start, end = 0, a
        else:
            start, end = a, b
        if step > 0:
            from operator import lt as cmp
        elif step < 0:
            from operator import gt as cmp
        else:
            raise ValueError("xrange() arg 3 must not be zero")
        from itertools import count, takewhile
        return takewhile(
            lambda n: cmp(n, end),
            (start + i*step for i in count()))

This should give you a larger range while still avoiding any significant 
overhead when you don't need it.



-- 
Steven



More information about the Python-list mailing list