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

Stefano Borini stefano.borini at gmail.com
Wed Apr 17 02:38:00 EDT 2019


I don't like it either. Ideally, I would want "returning", but of
course a new keyword is not an option for such limited use case.
"as" is probably much better, and the behavior of as in other contexts
is very similar.

On Tue, 16 Apr 2019 at 23:42, Jan Kaliszewski <zuo at kaliszewski.net> wrote:
>
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Kind regards,

Stefano Borini


More information about the Python-ideas mailing list