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


Groups > comp.lang.python > #51718

Oddity with 'yield' as expression - parentheses demanded

Date 2013-08-01 07:25 +0100
Subject Oddity with 'yield' as expression - parentheses demanded
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.65.1375338358.1251.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web