classification
Title: yield expression vs lambda
Type: behavior Stage: patch review
Components: Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, georg.brandl
Priority: high Keywords: needs review, patch

Created on 2008-12-26 09:57 by georg.brandl, last changed 2008-12-27 18:24 by benjamin.peterson. This issue is now closed.

Files
File name Uploaded Description Edit
nasty_lambda_generators.patch benjamin.peterson, 2008-12-27 02:02
Messages (6)
msg78291 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-26 09:57
With lambda, the ban on "return x" in generators can be evaded:

>>> x = lambda: ((yield 1), (yield 2))
>>> list(x())
[1, 2, (None, None)]
>>> dis.dis(x)
  1           0 LOAD_CONST               0 (1)
              3 YIELD_VALUE
              4 LOAD_CONST               1 (2)
              7 YIELD_VALUE
              8 BUILD_TUPLE              2
             11 RETURN_VALUE
msg78332 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-27 02:02
Attaching patch?

BTW, how did you find this bug? :)
msg78370 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-27 17:41
I didn't find it, but someone from the German Python webforum. :)

Hmm, I wonder why lambda: (yield 1) alone doesn't give [1, None]. (That
should also go into the test case.)

Anyway, perhaps yield in lambdas should be forbidden.
msg78373 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-27 18:07
On Sat, Dec 27, 2008 at 11:41 AM, Georg Brandl <report@bugs.python.org> wrote:
>
> Georg Brandl <georg@python.org> added the comment:
>
> I didn't find it, but someone from the German Python webforum. :)
>
> Hmm, I wonder why lambda: (yield 1) alone doesn't give [1, None]. (That
> should also go into the test case.)

Actually, I don't think the return value should even make it's way to
the list. Generator lambdas shouldn't have any return value IMO.

>
> Anyway, perhaps yield in lambdas should be forbidden.

Probably too late now for 2.x and 3.x.
msg78374 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-27 18:15
>> Hmm, I wonder why lambda: (yield 1) alone doesn't give [1, None]. (That
>> should also go into the test case.)
>
> Actually, I don't think the return value should even make it's way to
> the list. Generator lambdas shouldn't have any return value IMO.

Yes, I think so too. I just wondered at the seemingly inconsistent
behavior right now.

>> Anyway, perhaps yield in lambdas should be forbidden.
> 
> Probably too late now for 2.x and 3.x.

Right. I'd say go for applying the patch.
msg78377 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-27 18:24
Fixed in r67954.
History
Date User Action Args
2008-12-27 18:24:29benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg78377
2008-12-27 18:15:39georg.brandlsetmessages: + msg78374
2008-12-27 18:07:40benjamin.petersonsetmessages: + msg78373
2008-12-27 17:41:42georg.brandlsetmessages: + msg78370
2008-12-27 02:02:57benjamin.petersonsetfiles: + nasty_lambda_generators.patch
nosy: + benjamin.peterson
messages: + msg78332
keywords: + needs review, patch
type: behavior
stage: patch review
2008-12-26 09:57:38georg.brandlcreate