xrange issue 7721
Mark Lawrence
breamoreboy at yahoo.co.uk
Sat May 29 19:49:11 EDT 2010
Hi Martin, thanks for the response, please see below.
On 29/05/2010 20:12, Martin Manns wrote:
> On Sat, 29 May 2010 19:46:28 +0100
> Mark Lawrence<breamoreboy at yahoo.co.uk> wrote:
>
>> I've had an OverflowError using xrange with Python 2.6.5 on Windows.
>> Googling got me to the subject line.
>>
>> msg97928 gives a code snippet to overcome the limitations of xrange,
>> allowing for negative steps, however it doesn't raise a ValueError
>> for a zero step. msg99624 gives a docs change that has been
>> implemented for V2.6, but this doesn't refer to the msg97928 code
>> snippet, rather it refers to a one liner that only works for positive
>> steps. The docs for V2.7 haven't been changed at all.
>
> Mark:
>
> Thank you for posting.
>
> 2.7 is not affected by issue 7721 because itertools.islice behavior is
> changed. Therefore, the original snippet should work in 2.7 (I have not
> tested this).
From http://docs.python.org/dev/library/itertools.html
"Unlike regular slicing, islice() does not support negative values for
start, stop, or step."
Rule 1 of programming never assume anything, particularly wrt testing. I
assume that you are ok with this. :) Dreadful I know :)
>
> I found the msg97928 code pretty obvious when seeing the snippet.
> If you disagree you may consider re-opening the issue.
Try running this on Python 2.6.5 in file irange.py
from itertools import takewhile, count
def irange(start, stop, step):
if step < 0:
cond = lambda x: x > stop
else:
cond = lambda x: x < stop
return takewhile(cond, (start + i * step for i in count()))
if __name__=='__main__':
for i in irange(0, 10, 0): print i
My output from the command line
c:\Users\Mark\python>irange
0
0
etc etc etc
I trust that you get my point regarding the failure to raise a
ValueError :) Or am I wearing my extremely stupid hat today?
>
> Martin
>
Kindest regards.
Mark Lawrence.
More information about the Python-list
mailing list