[Python-Dev] PEP 322: Reverse Iteration

Guido van Rossum guido at python.org
Tue Nov 4 10:53:08 EST 2003

> And what about irange with an optional reverse= argument?  I did have
> (and write about on c.l.py) a case where I currently code:
>     if godown:
>         iseq = xrange(len(sq)-1, start-1, -1)
>     else:
>         iseq = xrange(start, len(sq), 1)
>     for index in iseq:
>         ...
> and would be just delighted to be able to code, instead,
>     for index in irange(start, len(sq), reverse=godown):
>         ...
> Even when the need to reverse can more easily be hardwired in
> the source (a more common case), would
>     for index in irange(start, stop, reverse=True):
> be really so much worse than
>     for index in revrange(start, stop):
> ...?

Darn.  At your recommendation I tried reading my inbox in reverse
today (how appropriate... :-) and I missed this use case when I said
I'd rather have two functions.  Oh well.

I do think that the savings in typing from having a reverse= keyword
for that one use case are easily outnumbered by the extra typing for
the much more common use case that has reverse=True.

But really, I could live with either one, so Raymond can decide based
upon the evidence, and as I said, either way having this in itertools
is an argument against making reversed() a builtin.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list