[docs] [issue12003] documentation: alternate version of xrange seems to fail.

Terry J. Reedy report at bugs.python.org
Sat May 7 09:23:50 CEST 2011


Terry J. Reedy <tjreedy at udel.edu> added the comment:

I verified bug on winxp with 2.7
xrangef [5, 4, 3, 2, 1, 0]
py26 []
py27 [5, 4, 3, 2, 1, 0, -1, -2]
v1 [5, 4, 3, 2, 1, 0]
v2 [5, 4, 3, 2, 1, 0]
v3 [5, 4, 3, 2, 1, 0]
-----------------------
xrangef [5, 3, 1, -1, -3]
py26 []
py27 [5, 3, 1, -1, -3, -5]
v1 [5, 3, 1, -1, -3]
v2 [5, 3, 1, -1, -3]
v3 [5, 3, 1, -1, -3]

The problem is that -1 should instead be +1 for negative steps. The 2.6 version completely failed for negative steps because the comparison needs to be reversed. The doc example could add "for positive steps. For negative steps, use '+1' instead of '-1'". Or change expression to
islice(count(start, step), (stop-start+step-1+2*(step<0))//step)
(the addition is  +2*(step<0)).
Or change -1 to +(-1 if step>0 else 1). I tested both.

----------
nosy: +terry.reedy

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12003>
_______________________________________


More information about the docs mailing list