[Python-ideas] Catching the return value of a generator at the end of a for loop

Jan Kaliszewski zuo at kaliszewski.net
Tue Apr 16 18:32:13 EDT 2019


Hello,

2019-04-16 Stefano Borini <stefano.borini at gmail.com> dixit:

> def g():
>     yield 2
>     yield 3
>     return 6
[...]
> for x in g() return v:
>     print(x)
> 
> print(v) # prints 6

I like the idea -- occasionally (when dealing with `yield
from`-intensive code...) I wish such a shortcut existed.

I don't like the proposed keyword. Maybe `as` would be better?

    for x in g() as v:
        print(x)

    print(v)

Cheers,
*j


More information about the Python-ideas mailing list