Re: Creating lambdas inside generator expression

2022-06-30 Thread Peter Otten
On 29/06/2022 23:17, Chris Angelico wrote: On Thu, 30 Jun 2022 at 02:49, Johannes Bauer wrote: But now consider what happens when we create the lambdas inside a list comprehension (in my original I used a generator expresison, but the result is the same). Can you guess what happens when we crea

Re: Creating lambdas inside generator expression

2022-06-29 Thread Chris Angelico
On Thu, 30 Jun 2022 at 02:49, Johannes Bauer wrote: > But now consider what happens when we create the lambdas inside a list > comprehension (in my original I used a generator expresison, but the > result is the same). Can you guess what happens when we create conds > like this? > > conds = [ lamb

Re: Creating lambdas inside generator expression

2022-06-29 Thread Antoon Pardon
Or you could try this as an alternative: conds = [ (lambda code: lambda msg: msg.hascode(code))(z) for z in ("foo", "bar") ] Op 29/06/2022 om 12:43 schreef Johannes Bauer: Aha! conds = [ lambda msg, z = z: msg.hascode(z) for z in ("foo", "bar") ] Is what I was looking for to explicitly use

Creating lambdas inside generator expression

2022-06-29 Thread Johannes Bauer
Hi list, I've just encounted something that I found extremely unintuitive and would like your feedback. This bit me *hard*, causing me to question my sanity for a moment. Consider this minimal example code (Py 3.10.4 on Linux x64): class Msg(): def hascode(self, value): p

Re: Creating lambdas inside generator expression

2022-06-29 Thread Johannes Bauer
Aha! conds = [ lambda msg, z = z: msg.hascode(z) for z in ("foo", "bar") ] Is what I was looking for to explicitly use the value of z. What a caveat, didn't see that coming. Learning something new every day. Cheers, Joe Am 29.06.22 um 11:50 schrieb Johannes Bauer: > Hi list, > > I've just en