How about adding slice notation to iterators/generators?

Eloff dan.eloff at gmail.com
Fri Oct 16 17:28:50 EDT 2009


On Oct 16, 3:54 am, Terry Reedy <tjre... at udel.edu> wrote:
> There is already an obvious standard way to do this.
>
> it = <whatever>
> next(it) #toss first item
> for item in it:
>   ....
>

That fails if there is no first item. You're taking one corner case
and saying there's an easy way to do it, which is more or less true,
but you miss my point that I'm suggesting we make the general case
easier.

By giving iterators a default, overridable, __getitem__ that is just
syntactic sugar for islice, they would share a slightly larger
interface subset with the builtin container types. In a duck-typed
language like python, that's almost always a good thing. You could use
iterators in more situations which expect something more like a list.

As long as it breaks no rationally existing code, I can think of no
good reason why not to do this in a future python.

-Dan



More information about the Python-list mailing list