transforming a list into a string

Tim Peters tim.peters at gmail.com
Sun Aug 1 18:59:42 EDT 2004


[Christopher T King]
> ...
> Curious, why isn't slicing of generators defined, using islice(), so "it1
> = iter(items)[0::2]" is valid?

The real question then is why iterators don't, because a
generator-function returns a generator-iterator, and the latter
supplies only the methods in the general iterator protocol (next() and
__iter__()).  That protocol was deliberately minimal, to make it easy
for all kinds of objects to play along.  islice() was invented long
after.  Now that islice() exists, it may indeed make sense to use it
to give a meaning to slice notation applied to iterators.  But doing
so requires that iterators implement the appropriate type slots to
"look like" they're also "sequences" (that's how dispatching for slice
notation works), and that's a lot more to ask of objects.  If only
some iterators implement it (like generator-iterators), then the
general interchangeability of iterable objects we enjoy today would be
damaged too.



More information about the Python-list mailing list