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


Groups > comp.lang.python > #51745

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'syntax': 0.04; 'say,': 0.05; 'assignment': 0.07; 'expressions': 0.07; "subject:' ": 0.07; 'adopted': 0.09; 'consistency': 0.09; 'omit': 0.09; 'so?': 0.09; 'statements': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '"is': 0.16; '(x,': 0.16; 'bind': 0.16; 'brackets:': 0.16; 'comma.': 0.16; 'distinct': 0.16; 'expression,': 0.16; 'expression.': 0.16; 'governed': 0.16; 'lambda': 0.16; 'parentheses': 0.16; 'preserve': 0.16; 'semantically': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'syntaxerror:': 0.16; 'tuple': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; 'basically': 0.19; 'normally': 0.19; 'thu,': 0.19; 'meant': 0.20; '(in': 0.22; 'aug': 0.22; 'rules': 0.22; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '[1]': 0.29; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; '>>>>': 0.31; 'equivalent.': 0.31; 'sep': 0.31; 'subject:with': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'yield': 0.36; 'similar': 0.36; 'being': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'expression': 0.60; 'simply': 0.61; 'skip:n 10': 0.64; 'became': 0.64; 'places': 0.64; 'side': 0.67; 'invalid': 0.68; 'sole': 0.78; 'hand': 0.80; 'is)': 0.84; 'presumably': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=x5nvDBltR9yK7NtVzO51MISMo9xUj263PHClZNbsUKc=; b=iKisLSBNAJugRj15Enzb5Hh8D77UtZiYhatP5CDDuBboQn6PPAeHBNTkLZbyXSffGV hQs4dWtFqtve2cVEdGno0Hxihmv9xwq1gAdWLWfa425kaxoozOrp/XulbWle7AUX+60o ZeUGLiJt1wjNKexDLgGWBF4+htgatotjWjFaRSQBzfsWeIHRpcXhYoyhq9NJeEp/DEGv V0p5irJXk7D3lZbxA3A3YO0zUD3Zm7RfJn7waaCKSS0leeYHZrZ48NZlhY5m5jVlPNJ7 RrLQYBIbAuAApwBqQnHzadX3oukYh8+W8LwOa/+QygEE/ytwHZ5QN+DywYuHXk/N96dx mwjA==
X-Received by 10.66.102.1 with SMTP id fk1mr5601750pab.90.1375378595481; Thu, 01 Aug 2013 10:36:35 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAPTjJmoCj40Gbktu-0Tkos_toTWvPJ8_0r61FC9wU0GA9gMDMw@mail.gmail.com>
References <CAPTjJmoCj40Gbktu-0Tkos_toTWvPJ8_0r61FC9wU0GA9gMDMw@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 1 Aug 2013 11:35:54 -0600
Subject Re: Oddity with 'yield' as expression - parentheses demanded
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.83.1375378604.1251.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375378604 news.xs4all.nl 15993 [2001:888:2000:d::a6]:47229
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51745

Show key headers only | View raw


On Thu, Aug 1, 2013 at 12:25 AM, Chris Angelico <rosuav@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Oddity with 'yield' as expression - parentheses demanded Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-01 11:35 -0600

csiph-web