Message306940
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) |
|
Date |
User |
Action |
Args |
2017-11-25 04:34:28 | ncoghlan | set | recipients:
+ ncoghlan, gvanrossum, georg.brandl, rhettinger, belopolsky, benjamin.peterson, erickt, glyph, Inyeol.Lee, serhiy.storchaka, yselivanov, esc24, danielsh, emptysquare, levkivskyi, Jim Fasarakis-Hilliard |
2017-11-25 04:34:28 | ncoghlan | set | messageid: <1511584468.88.0.213398074469.issue10544@psf.upfronthosting.co.za> |
2017-11-25 04:34:28 | ncoghlan | link | issue10544 messages |
2017-11-25 04:34:28 | ncoghlan | create | |
|