Oddity with 'yield' as expression - parentheses demanded

Ian Kelly ian.g.kelly at gmail.com
Thu Aug 1 13:35:54 EDT 2013


On Thu, Aug 1, 2013 at 12:25 AM, Chris Angelico <rosuav at gmail.com> wrote:
> Was playing around with yield inside a lambda and ran into a distinct oddity.
>
> Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
> 32 bit (Intel)] on win32
>>>> foo=lambda x: yield(x)
> SyntaxError: invalid syntax
>>>> def foo(x):
>         return yield(x)
> SyntaxError: invalid syntax
>>>> def foo(x):
>         x=yield(x)
>         return x
>>>> foo=lambda x: (yield x)
>
> If yield is an expression, why does it need extra parentheses around
> it? [1] suggest that "(yield x)" is an expression that can elide the
> parens only when it "is the sole expression on the right hand side of
> an assignment statement", and presumably there's a similar rule
> allowing the non-expression form "yield x" to omit the parens. Why is
> this so? Why is it not simply an expression on its own?

yield was a statement before it became an expression, and the syntax
"yield x, y, z" was (and still is) perfectly legal, with all three
expressions (technically a single tuple expression) being governed by
the yield.  That is to say, "yield x, y, z" and "yield (x, y, z)" are
semantically equivalent.  When it became an expression, in order to
preserve this equivalence, that meant that the yield expression needed
to bind even less tightly than the comma.  In terms of the grammar,
yield needed to take an expression_list, not just an expression.

There are only three places in the grammar where expression_lists are
used without enclosing them in brackets:  expression statements (in
this case analogous to the yield statement), the return statement (not
normally used to return a value in a generator), and the assignment
statements.  So for consistency and clarity the rules for
parenthesizing yield statements are basically adopted from the
existing rules for parenthesizing expression_lists.



More information about the Python-list mailing list