[Python-Dev] Re: PEP 322: Reverse Iteration
Guido van Rossum
guido at python.org
Wed Nov 5 13:43:16 EST 2003
> > Yes. I'm getting cold feet about __reversed__.
> What can I do to warm those feet?
> I spent a month making this proposal as perfect as possible, gathering
> support for it, trying each proposed modification, and enduring what
> feels like hazing. Still, there is a little bit of energy left if that
> what it takes to put the ball over the goal line.
> Getting this far hasn't been easy. Python people are quick to express
> negativity on just about anything and they take great pleasure is
> exploring every weird variant they can think of.
I'm okay with adding reversed() as a builtin that works for sequences
only but I'm not okay with adding the __reversed__ protocol.
For me, the main advantage of reversed() is that it expresses better
what I mean when I'm going over a list (or other concrete sequence)
backwards. The __reversed__ protocol muddles the issue by inviting to
try to make reversed() work for some iterators; I don't see the use
case (or if I do see it, I see it as much less important than the
> > I also still think that a reversed [x]range() would give us a bigger
> > bang for the buck
> I'm not willing to go that route:
> * Several posters gave negative feedback on that option.
> * It doesn't address the ugly and inefficient s[::-1] approach which I
> really do not want to become *the* idiom.
> * Providing yet another variant of xrange() is a step backwards IMO.
> * It is not an extensible protocol like the reversed() / __reversed__
> * Except for the simple case of revrange(n), the multiple argument forms
> are not a simplification (IMO) and are still difficult to visually
> verify (try the example from random.shuffle).
> * A unique benefit to python is the ability to loop over containers
> without using indices. The current proposal supports that idea. The
> revrange() approach doesn't.
Points well taken. About your last bullet, I wonder if one of the
issues is that when doing a forward loop over a container, we don't
really care that much about the order as long as we get all items
(witness the popularity of looping over dicts). But when doing a
reverse loop, we clearly *do* care about the order. So forward and
reverse iteration are not symmetric. This may explains why 3 out of 5
examples you found *need* the index.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev