[issue10544] yield expression inside generator expression does nothing
Nick Coghlan
report at bugs.python.org
Fri Nov 24 23:34:28 EST 2017
Nick Coghlan <ncoghlan at gmail.com> added the comment:
I realised that even without modifying the compiler first, I could illustrate the proposed `yield from` based resolution for the comprehension case by way of explicit yield from clauses:
```
def get_gen_result(gen, inputs):
try:
yield_value = next(gen)
for send_value in inputs:
print(f"Received: {yield_value}; Sending: {send_value}")
yield_value = gen.send(send_value)
except StopIteration as exc:
return exc.value
raise RuntimeError("Failed to exhaust generator")
def example():
comp1 = yield from [str((yield x)) for x in ('1st', '2nd')]
comp2 = yield from [int((yield x)) for x in ('3rd', '4th')]
return comp1, comp2
>>> result = get_gen_result(example(), range(4))
Received: 1st; Sending: 0
Received: 2nd; Sending: 1
Received: 3rd; Sending: 2
Received: 4th; Sending: 3
>>> result
(['0', '1'], [2, 3])
```
So if we decided to make yield-in-a-comprehension imply the use of yield from, we'd only need:
- DeprecationWarning in 3.7 to say "this is going to imply 'yield from (comprehension)' in 3.8+"
- making the 'yield from' implicit in 3.8 (thus ensuring that comprehensions always return the correct container type, even when they include yield expressions)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue10544>
_______________________________________
More information about the Python-bugs-list
mailing list