[Python-ideas] Propagating StopIteration value
Mike Graham
mikegraham at gmail.com
Sat Oct 6 22:47:36 CEST 2012
On Sat, Oct 6, 2012 at 4:10 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> As StopIteration now have value, this value is lost when using functions
> which works with iterators/generators (map, filter, itertools). Therefore,
> wrapping the iterator, which preserved its semantics in versions before 3.3,
> no longer preserves it:
>
> map(lambda x: x, iterator)
> filter(lambda x: True, iterator)
> itertools.accumulate(iterator, lambda x, y: y)
> itertools.chain(iterator)
> itertools.compress(iterator, itertools.cycle([True]))
> itertools.dropwhile(lambda x: False, iterator)
> itertools.filterfalse(lambda x: False, iterator)
> next(itertools.groupby(iterator, lambda x: None))[1]
> itertools.takewhile(lambda x: True, iterator)
> itertools.tee(iterator, 1)[0]
>
> Perhaps it would be worth to propagate original exception (or at least it's
> value) in functions for which it makes sense.
Can you provide an example of a time when you want to use such a value
with a generator on which you want to use one of these so I can better
understand why this is necessary? the times I'm familiar with wanting
this value I'd usually be manually stepping through my generator.
Mike
More information about the Python-ideas
mailing list