[Python-Dev] Re: PEP 322: Reverse Iteration
Phillip J. Eby
pje at telecommunity.com
Wed Nov 5 12:02:09 EST 2003
At 07:28 AM 11/5/03 -0800, Guido van Rossum wrote:
> > Why not change enumerate() to return an iterable, rather than an
> > iterator? Then its __reversed__ method could attempt to delegate to
> > the underlying iterable. Is it likely that anyone relies on
> > enumerate() being an iterator, rather than an iterable?
>I find it rather elegant to use enumerate() on a file to generate line
>numbers and lines together (adding 1 to the index to produce a more
>conventional line number). What's more elegant than
> for i, line in enumerate(f):
> print i+1, line,
>to print a file with line numbers??? I've used this in throwaway
>code at least, and would hate to lose it.
I thought 'for x in y' always called 'iter(y)', in which case the above
still works. It's only this:
ef = enumerate(f)
i,line = ef.next()
print i+1, line,
That would break.
More information about the Python-Dev