[Python-ideas] Propagating StopIteration value

Serhiy Storchaka storchaka at gmail.com
Mon Oct 8 23:22:57 CEST 2012


On 07.10.12 22:18, Richard Oudkerk wrote:
> That means that all but the last return value is ignored.  Why is the
> last return value any more important than the earlier ones?

Because I think the last return value more useful for idiom

   lookahead = next(iterator)
   process(lookahead)
   iterator = itertools.chain([lookahead], iterator)

> ISTM it would make just as much sense to do
>
>    def chain(*iterables):
>        values = []
>        for it in iterables:
>            values.append(yield from it)
>        return values

It changes the behavior for iterators. And now more difficult to get a 
generator which yields and returns the same values as the original. We 
need yet one wrapper.

   def lastvalue(generator):
       return (yield from generator)[-1]

   iterator = lastvalue(itertools.chain([lookahead], iterator))

Yes, it can work.





More information about the Python-ideas mailing list