beginning index for generators
tuure at laurinolli.net
Sun Oct 17 04:56:32 CEST 2004
> I was wondering if there is or there could be some way to pass a generator an
> optional starting index so that if it supported that slicing could be made
> more efficient. Right now if you do use a generator and do a [100:110] or any
> other kind of slice it reads all the values up to 100 also.
> I know a lot of generators have to do all previous parts before they can do
> the next part but it would be a nice optimization that can start at any index
> to take an optional argument to tell them what index they are currently
> working on. This could make strides more efficient also since it would not
> have to run the generator to skip the parts it was not using.
I think adding an optional .skip(n) to the fenerator protocol could be
useful, it could be used for strides too, and I can't think of a much
more generic way to do things.
However, the caller willing to use this optimization would always need
to check if .skip() is defined, although this could naturally be
automated to some degree.
It could be specified as:
If an iterator object has a function skip([n=1]), it can be used to
advance the iterator n steps without it having to yield the intermediate
values. The end state of the iterator should be the same as with:
for i in range(iterable):
More information about the Python-list