[Python-Dev] PEP 322: Reverse Iteration
Samuele Pedroni
pedronis at bluewin.ch
Tue Nov 4 19:47:33 EST 2003
At 16:07 04.11.2003 -0800, Guido van Rossum wrote:
> > > > for i, value in reversed(enumerate(listofnum)):
> > >
> > > Sorry, this doesn't work. enumerate() returns an iterator, reversed()
> > > requires a sequence.
> >
> > I believe the assumption is that enumerate (as well as the proposed
> irange)
> > would grow an __reversed__ method to handle just that usage.
>
>Ah, so it is. Then the PEP's abstract is wrong:
>
>"""
>This proposal is to add a builtin function to support reverse
>iteration over sequences.
>"""
>
>Also, the PEP should enumerate (:-) which built-in types should be
>modified in this way, to give an impression of the enormity (or not)
>of the task.
what is not clear to me is that the PEP is explicit about reversed()
refusing general iterables and in particular infinite iterators, but then
the combination reversed enumerate.__reversed__ would accept them or not?.
Will enumerate implement __reversed__ in terms of keeping the enumerate
argument around instead of just a iterator derived from it and reproducing
then the reversed behavior: limits checks and implementation strategy on
the original argument if/when __reversed__ is called?
so
for x in reversed(enumerate(itertools.count())):
pass
would throw an exception instead of not terminating, OTHERWISE with the
strategy of consuming
the iterator if x is a finite iterator but without __len__ then
reversed(x) would not work but
reversed(enumerate(x)) would.
Further enumerate.__iter__ does not enable re-iteration, simply it does not
return a fresh iterator but what about enumerate.__reversed__ ?
regards.
More information about the Python-Dev
mailing list