[Python-ideas] Propagating StopIteration value
Serhiy Storchaka
storchaka at gmail.com
Sun Oct 7 21:06:29 CEST 2012
On 07.10.12 05:11, Greg Ewing wrote:
> It's highly debatable whether this is even wrong. The purpose
> of StopIteration(value) is for a generator to return a value
> to its immediate caller when invoked using yield-from. The
> value is not intended to propagate any further than that.
If immediate caller can propagate generated values with the help of
"yield from", why it can not propagate returned from "yield from" value?
> A non-iterator analogy would be
>
> def f():
> return 42
>
> def g():
> f()
No, a non-iterator analogy would be
g = functools.partial(f)
or
g = functools.lru_cache()(f)
I expect g() to return 42 here.
And it will be expected and useful if
yield from itertools.chain([prefix], iterator)
will return the same value as
yield from iterator
Now chain equivalent to:
def chain(*iterables):
for it in iterables:
yield from it
I propose make it equivalent to:
def chain(*iterables):
value = None
for it in iterables:
value = yield from it
return value
More information about the Python-ideas
mailing list