PEP 322: Reverse Iteration (REVISED, please comment)

Raymond Hettinger python at rcn.com
Tue Oct 28 13:22:44 EST 2003


Based on your extensive feedback, PEP 322 has been completely revised.
The response was strongly positive, but almost everyone preferred
having a function instead of multiple object methods.  The updated
proposal is at:

    www.python.org/peps/pep-0322.html

In a nutshell, it proposes a builtin function that greatly simplifies reverse
iteration.  The core concept is that clarity comes from specifying a
sequence in a forward direction and then saying, "inreverse()":

    for elem in inreverse(seqn):
         . . .

Unlike seqn[::-1], this produces a fast iterator instead of a full reversed
copy.

Discussions with Guido made it clear that inreverse() will not be extended
to cover all iterables.  The proposal is about simplicity, expression, and
performance.  As such, it would be counter-productive to take in a general
iterable, run it to completion, save the data in memory, and then iterate
over the data in reverse.


Raymond Hettinger




More information about the Python-list mailing list