New submission from Chris Angelico: As yield is an expression, it's legal in a lambda function, which then means you have a generator function. But it's not quite the same as the equivalent function made with def:
$ python3 Python 3.5.0a0 (default:1c51f1650c42+, Dec 29 2014, 02:29:06) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> f=lambda: (yield 5) >>> x=f() >>> next(x) 5 >>> x.send(123) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> def f(): return (yield 5) ... >>> x=f() >>> next(x) 5 >>> x.send(123) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration: 123 >>> x = (lambda: print((yield 1)) or 2)() >>> next(x) 1 >>> x.send(3) 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration The last example demonstrates that send() is working, but the return value is not getting propagated. Disassembly shows this: >>> dis.dis(lambda: (yield 5)) 1 0 LOAD_CONST 1 (5) 3 YIELD_VALUE 4 POP_TOP 5 LOAD_CONST 0 (None) 8 RETURN_VALUE >>> def f(): return (yield 5) ... >>> dis.dis(f) 1 0 LOAD_CONST 1 (5) 3 YIELD_VALUE 4 RETURN_VALUE I'm sure this is a bug that will affect very approximately zero people, but it's still a peculiar inconsistency! Verified with 3.5 and 3.4. ---------- components: Interpreter Core messages: 233662 nosy: Rosuav priority: normal severity: normal status: open title: Generator return value ignored in lambda function versions: Python 3.4, Python 3.5 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue23192> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com