Author Rosuav
Recipients Rosuav
Date 2015-01-08.14:42:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1420728125.93.0.910147391239.issue23192@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-01-08 14:42:05Rosuavsetrecipients: + Rosuav
2015-01-08 14:42:05Rosuavsetmessageid: <1420728125.93.0.910147391239.issue23192@psf.upfronthosting.co.za>
2015-01-08 14:42:05Rosuavlinkissue23192 messages
2015-01-08 14:42:05Rosuavcreate