[Python-Dev] [SPAM: 3.000] [issue11682] PEP 380 reference implementation for 3.3

Guido van Rossum guido at python.org
Thu Nov 10 00:11:22 CET 2011

On Wed, Nov 9, 2011 at 2:13 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>> I see this as inevitable. By the time the parser sees 'yield' it has
>> made its choices; the 'from' keyword cannot modify that. So whenever
>> "yield expr" must be parenthesized, "yield from expr" must too.
> This is patently untrue, because by version of the grammar
> allows 'f(yield from x)', while disallowing 'f(yield x)'.
> I made a conscious decision to do that, and I'm a bit alarmed
> at this decision being overridden at the last moment with no
> debate.

We're having the debate now. :-)

I can't find anywhere in the PEP where it says what the operator
priority of "yield from" is, so you can't blame me for thinking the
priority should be the same as for "yield".

>> At the same time, "yield expr, expr" works;
> Um, no, that's a syntax error in any context, as far as I
> can see.

Actually it is valid, meaning "yield (expr, expr)" in any context
where "yield expr" is valid (please let me know if there are any
contexts where that isn't true):

>>> def foo():
...  yield 1, 1
>>> def foo():
...  if (yield 1, 1): pass
>>> def foo():
...   bar((yield 1, 1))
>>> def foo():
...  x = yield 1, 1

>> but does "yield from expr, expr" mean anything?
> In my current grammar, it's a syntax error on its own,
> but 'f(yield from x, y)' parses as 'f((yield from x), y)',
> which seems like a reasonable interpretation to me.

Once you realize that "yield from x, y" has no meaning, sure. But
without thinking deeper about that I can't prove that we'll never find
a meaning for it. We had a similar limitation for "with a, b:" --
initially it was illegal, eventually we gave it a meaning.

> What's not quite so reasonable is that if you have an
> expression such as
>   f(x) + g(y)
> and you decide to turn f into a generator, the obvious
> way to rewrite it would be
>   yield from f(x) + g(y)
> but that unfortunately parses as
>   yield from (f(x) + g(y))

Well duh. :-)

> If I'd thought about this more at the time, I would
> probably have tried to make the argument to yield-from
> something further down in the expression hierarchy,
> such as a power. That might be tricky to achieve
> while keeping the existing behaviour of 'yield',
> though.

IMO that would be the wrong priority for a keyword-based operator.
Note that 'not' also has a very low priority.

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list