Allow `return yield from`
(I originally wrote this in bpo47147, but apparently because it has to go through python-ideas first?) I would like to be able to use a `yield from` expression in a `return` statement without parentheses, as a small quality of life tweak, i.e.: return yield from gen instead of return (yield from gen) I think this makes sense, since `yield from` can be used on the right-hand-side of an assignment, which accepts any expression, and so should `return`. Here is a medium-sized real-world example of where I'm using this, where it would be nice to allow `return yield from`:https://gist.github.com/pxeger/48f97484364bf0b43dee974a8f0f4265 Patrick
-1 Extra parentheses are not hard to type but this notation is much better readable. On Mon, Mar 28, 2022 at 11:06 PM Patrick Reader <python-ideas@pxeger.com> wrote:
(I originally wrote this in bpo47147, but apparently because it has to go through python-ideas first?)
I would like to be able to use a `yield from` expression in a `return` statement without parentheses, as a small quality of life tweak, i.e.:
return yield from gen
instead of
return (yield from gen)
I think this makes sense, since `yield from` can be used on the right-hand-side of an assignment, which accepts any expression, and so should `return`.
Here is a medium-sized real-world example of where I'm using this, where it would be nice to allow `return yield from`:https://gist.github.com/pxeger/48f97484364bf0b43dee974a8f0f4265
Patrick _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/L6XRQ5... Code of Conduct: http://python.org/psf/codeofconduct/
-- Thanks, Andrew Svetlov
On Mon, Mar 28, 2022 at 16:06 Patrick Reader <python-ideas@pxeger.com> wrote:
I would like to be able to use a `yield from` expression in a `return` statement without parentheses, as a small quality of life tweak, i.e.:
return yield from gen
instead of
return (yield from gen)
What does this do? `return (yield …)` is the expression form, so this generator is really a coroutine, but unless I missed it your gist doesn't have a `.send` or `.throw` and you aren't using the value of StopIteration. So I think your idea is really to create a shorthand for ``` yield from gen return ``` Is that what you mean? or if I misunderstand please explain further. - Mike I think this makes sense, since `yield from` can be used on the
right-hand-side of an assignment, which accepts any expression, and so should `return`.
Here is a medium-sized real-world example of where I'm using this, where it would be nice to allow `return yield from`:https://gist.github.com/pxeger/48f97484364bf0b43dee974a8f0f4265
Patrick
On 29/03/2022 03:18, Michael Smith wrote:
On Mon, Mar 28, 2022 at 16:06 Patrick Reader <python-ideas@pxeger.com> wrote:
I would like to be able to use a `yield from` expression in a `return` statement without parentheses, as a small quality of life tweak, i.e.:
return yield from gen
instead of
return (yield from gen)
What does this do? `return (yield …)` is the expression form, so this generator is really a coroutine, but unless I missed it your gist doesn't have a `.send` or `.throw` and you aren't using the value of StopIteration.
So I think your idea is really to create a shorthand for
``` yield from gen return ```
Is that what you mean? or if I misunderstand please explain further.
In my example, the return value (StopIteration value) of the generator is the function which will handle the next character in the lexer. The value of a `yield from` expression is the StopIteration value.
participants (3)
-
Andrew Svetlov
-
Michael Smith
-
Patrick Reader