Pre-PEP: reverse iteration methods

John Roth newsgroups at jhrothjr.com
Tue Sep 23 22:05:07 EDT 2003


"Andrew Dalke" <adalke at mindspring.com> wrote in message
news:hJ6cb.1252$RW4.107 at newsread4.news.pas.earthlink.net...
> Raymond Hettinger:
> > Proposal
> > ========
> >
> > Add a method called iter_backwards() to sequence objects that can
benefit
> > from it.  The above examples then simplify to::
> >
> >     for i in xrange(n).iter_backwards():
> >         print seqn[i]
> >
> >     for elem in seqn.iter_backwards():
> >         print elem
>
> What about letting 'iter_backwards' be a builtin which
> looks for __riter__ and if it doesn't exist, get the length
> and do old-fashioned offset indexing?

There are objects that support iteration that
don't support len(), such as file objects.
This has got the advantage, of course, that it would
automatically work on all objects that support
an sequence-like protocol, though.

Frankly, I prefer the notion of a method.
Maybe make it a method of object that
automatically works on all new style
objects that support a sequence-like
protocol?

John Roth







More information about the Python-list mailing list