[issue23192] Generator return value ignored in lambda function
Chris Angelico
report at bugs.python.org
Thu Jan 8 15:42:05 CET 2015
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 <report at bugs.python.org>
<http://bugs.python.org/issue23192>
_______________________________________
More information about the Python-bugs-list
mailing list