Issue 4748: yield expression vs lambda (original) (raw)

Created on 2008-12-26 09:57 by georg.brandl, last changed 2022-04-11 14:56 by admin. 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
2022-04-11 14:56:43 admin set github: 48998
2008-12-27 18:24:29 benjamin.peterson set status: open -> closedresolution: fixedmessages: +
2008-12-27 18:15:39 georg.brandl set messages: +
2008-12-27 18:07:40 benjamin.peterson set messages: +
2008-12-27 17:41:42 georg.brandl set messages: +
2008-12-27 02:02:57 benjamin.peterson set files: + nasty_lambda_generators.patchnosy: + benjamin.petersonmessages: + keywords: + needs review, patchtype: behaviorstage: patch review
2008-12-26 09:57:38 georg.brandl create