[Python-Dev] PEP 322: Reverse Iteration

Guido van Rossum guido at python.org
Wed Nov 5 10:33:24 EST 2003


> I think the search for the __reversed__ method is the meat of the
> proposal; I can define for myself a simple two-line generator that
> iterates in reverse over sequences.  What I need the language to
> define for me is a protocol for iterating over objects in reverse
> and for providing users of my own classes with the ability to
> iterate over them in reverse in a standard way.
> 
> If this proposal could be satisfied by the simple definition:
> 
> def reversed(seq):
>     for i in xrange(len(seq)-1, -1, -1):
>         yield seq[i]
> 
> I wouldn't be for it.  The reason I'm +1 is because I want a
> standard protocol for iterating in reverse over objects.

I would be *against* such a protocol.  It would end up complicating
almost everything that defines __iter__, for a very questionable
pay-off (reverse iteration isn't that common except for some special
cases).

The PEP got as far as it is by focusing on simplicity and sequences.
It is rapidly losing its innocence. :-)

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



More information about the Python-Dev mailing list