[Python-ideas] ``with from`` statement
Terry Reedy
tjreedy at udel.edu
Sat Dec 22 23:20:08 CET 2012
On 12/22/2012 2:09 PM, Guido van Rossum wrote:
> On Sat, Dec 22, 2012 at 10:26 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> On Sat, 22 Dec 2012 19:25:32 +0200
>> Andrew Svetlov <andrew.svetlov at gmail.com>
>> wrote:
>>> Python syntax looks like use of time machine day by day. I like it!
>>
>> Not sure I like "with yield from".
At the moment, that looks a bit dubious to me too. Maybe just because it
is new (to me).
>> How do you intend to explain that to
>> an average programmer?
>
> Break it down into pieces. The general form is
>
> with <expr>: <block>
with <expr> as <expr>: <block>
with yield from x() as y: ...
> where <expr> can take many forms, including
>
> yield from <expr>
>
> we just have to handwave a bit about the priorities,
Too much dependence on implicit priorities makes the language more
baroque and less clear. For instance, I am fine with having to
parenthesize generator expressions (except in calls where it would
result in doubled parens ((ge))). An explanation need more than a
handwave ;-).
> but that's usually okay. People do get
>
> x = yield from <expr>
No problem because = cleanly breaks the statement. More a problem is the
difference of x coming from a value yielded (or returned?) by the callee
instead of a value sent by the caller, as in x = yield y.
> It's just that currently somehow you have to surround "yield from
> <expr>" in an extra pair of parentheses everywhere except on the RHS
> of an assignment; my other pet peeve in this area is that you must
> write
>
> return (yield from <expr>)
>
> (which I end up writing fairly regularly).
I can see how that seems like a nuisance. Why that omitting parens
bother me less here? Perhaps because return binds the expression to
location of the call in the calling expression.
> I assume that if we can make the parens optional for assignment, we
> can make them optional in other places.
If the grammar can be written to do that sufficiently clearly, then it
should be explainable to people.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list