PEP 322: Reverse Iteration (second revision, please comment)

Jeremy Fincher tweedgeezer at hotmail.com
Thu Oct 30 14:48:42 EST 2003


"Raymond Hettinger" <vze4rx4y at verizon.net> wrote in message news:<cy3ob.33369$4O1.10013 at nwrdny01.gnilink.net>...
> * reversed() is being preferred to ireverse() as the best name.

I *really* don't like this name, especially with the list.sorted
that'll be going into 2.4.  People are going to either read this right
and read sorted wrong or vice versa.  Having list.sorted return a
sorted copy of its argument and having reversed return a reverse
iterator of its argument seems to be just asking for trouble
(especially when both are added in the same release).

And I really do think the name should imply that what you're getting
is an iterator.  I'd much prefer a keyword argument to iter/__iter__,
but I don't think it would be backwards compatible -- old __iter__s
would fail when given a "reverse" keyword.  But I do still think the
name should emphasize that we're getting an iterator.  How does
reviter and __reviter__ sound?  They're nice complements to iter and
__iter__, and they have precedence in

> * the sample implementation now clearly shows a check for a custom
>   reverse method and a guard against being applied to a mapping.

I was going to offer a counter-example of a balanced binary tree being
used as a mapping, until I realized you check for __reversed__ before
doing this check.  Still, though, it seems that the check there is
somewhat fragile.

> * expanded comments on the real world use cases to show what the
>    replacement code would look like.

In your atexit example, you say "In this application popping is
required, so the new function would not help."  The only reason
_run_exitfuncs pops is in order to iterate in reverse.  You might look
through the standard library for the idiom:

while someList:
  t = someList.pop()

Because that's exactly the idiom this will replace in a lot of
circumstances.

> Also, please take a look at the revrange() alternative to see if you
> prefer it or not.

I didn't see anything about revrange() in the PEP.

Jeremy




More information about the Python-list mailing list