Mix lambda and list comprehension?

R

Raymond Hettinger

Try this:
[lambda x, y=y:x+y for y in range(10)][4](2)
6


It is important to bind y in a closure at the time
the lambda is defined. Otherwise, y remains unbound
until you invoke the function call. At that time, the most
recent value of y is the last value in the range loop (namely, 9).


Raymond Hettinger



Peter Barth said:
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11
[lambda x:x+y for y in range(10)][4](2)
11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter
 
M

Michele Simionato

Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11
[lambda x:x+y for y in range(10)][4](2)
11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter

It is a scope issue. The last value for y is used for all
the created lambdas. All lambdas users are bitten by that,
soon or later. The solution is to make y local to the
lambda function, with the optional argument trick:
[lambda x,y=y:x+y for y in range(10)][4](2)
6

Michele
 
P

Peter Barth

Thanks a lot, works fine.
However, the solution does not really feel "pythonesque".
Is it considered a usability bug or fine as is?
- Peter

Raymond Hettinger said:
Try this:
[lambda x, y=y:x+y for y in range(10)][4](2)
6


It is important to bind y in a closure at the time
the lambda is defined. Otherwise, y remains unbound
until you invoke the function call. At that time, the most
recent value of y is the last value in the range loop (namely, 9).


Raymond Hettinger



Peter Barth said:
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11
[lambda x:x+y for y in range(10)][4](2)
11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter
 
M

Michele Simionato

[email protected] (Peter Barth) wrote in message news: said:
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11
[lambda x:x+y for y in range(10)][4](2)
11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter

It is a scope issue. The last value for y is used for all
the created lambdas. All lambdas users are bitten by that,
soon or later. The solution is to make y local to the
lambda function, with the optional argument trick:
[lambda x,y=y:x+y for y in range(10)][4](2)
6

or you could capture y as constants in the lambdas ;-)
[eval('lambda x:x+%s'%y) for y in range(10)][4](2)
6


Regards,
Bengt Richter

Thanks to God, there is a smile in your post!!

;)

Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top