Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51718 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2013-08-01 07:25 +0100 |
| Last post | 2013-08-01 07:25 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
Oddity with 'yield' as expression - parentheses demanded Chris Angelico <rosuav@gmail.com> - 2013-08-01 07:25 +0100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-08-01 07:25 +0100 |
| Subject | Oddity with 'yield' as expression - parentheses demanded |
| Message-ID | <mailman.65.1375338358.1251.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web