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

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Nov 9 23:13:15 CET 2011


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.

> At the same time, "yield expr, expr" works;

Um, no, that's a syntax error in any context, as far as I
can see.

> 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.

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))

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.

-- 
Greg


More information about the Python-Dev mailing list