Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51749 > unrolled thread

Re: Oddity with 'yield' as expression - parentheses demanded

Started byChris Angelico <rosuav@gmail.com>
First post2013-08-01 18:58 +0100
Last post2013-08-01 18:58 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Oddity with 'yield' as expression - parentheses demanded Chris Angelico <rosuav@gmail.com> - 2013-08-01 18:58 +0100

#51749 — Re: Oddity with 'yield' as expression - parentheses demanded

FromChris Angelico <rosuav@gmail.com>
Date2013-08-01 18:58 +0100
SubjectRe: Oddity with 'yield' as expression - parentheses demanded
Message-ID<mailman.85.1375379916.1251.python-list@python.org>
On Thu, Aug 1, 2013 at 6:35 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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.

Ahh, right. That makes good sense.

If this were being created anew now, would yield be made to bind more
tightly than the comma? That would mean that

yield x, y, z    # Yield a tuple

would need to be written as:

yield (x, y, z)

which wouldn't, in my opinion, be a bad thing. (It'd probably catch a
few people out, but a linter could notice that a tuple is being
created and ignored.) So this is a bit of a wart, but it's for
backward compatibility. Right?

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web