Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'win32': 0.03; 'syntax': 0.04; 'assignment': 0.07; "subject:' ": 0.07; 'omit': 0.09; 'so?': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '"is': 0.16; 'distinct': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'parentheses': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'syntaxerror:': 0.16; 'bit': 0.19; '>>>': 0.22; '[1]': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'sep': 0.31; 'url:python': 0.33; 'subject:with': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'similar': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'url:3': 0.61; 'simply': 0.61; 'skip:n 10': 0.64; 'side': 0.67; 'invalid': 0.68; 'sole': 0.78; 'hand': 0.80; 'presumably': 0.84; 'url:reference': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=ZXGTNanJBAGMlH0pd/5HMPd4xyu0p++rwOAj59wLtDI=; b=A+hvcIAbAEzuhCOi+Cke/35WlX8BaJ7oOSIxpz3dFP2E15NFZobae6N2XvI2CPV9Sf uQhSBCbSqzEwCFyhu4G6AOluIeYVyRPJkgQqCzc3WV8OPvAmoDxwshAQzt1YSZzG2eCP 90p4ReDApLSoiP206grnDOmuXfZ+0N/mUCxiF2cM87gt1lCnOl3Fam06jJxLtkUlQNfE /3c/WdBHnBKi+RoEgZpXM4Kix9UKQ9+7ac48PhWt/1QI95XeWB1lZKIRjFdQlO8nPgYG xzy/md4Xp8sQ/vYpFYEJPZvPinGQPZYB1PbE9ghQfv0MpAwIsUpDg8Hb4CJQAmeHzhdA ZiLw== MIME-Version: 1.0 X-Received: by 10.52.27.207 with SMTP id v15mr5181827vdg.83.1375338355914; Wed, 31 Jul 2013 23:25:55 -0700 (PDT) Date: Thu, 1 Aug 2013 07:25:55 +0100 Subject: Oddity with 'yield' as expression - parentheses demanded From: Chris Angelico To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375338358 news.xs4all.nl 15870 [2001:888:2000:d::a6]:32866 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51718 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? [1] http://docs.python.org/3.3/reference/expressions.html#grammar-token-yield_expression ChrisA