By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

fun with lambdas

P: n/a
Hello!

given the definition

def f(a,b): return a+b

With this code:

fs = [ lambda x: f(x,o) for o in [0,1,2]]

or this

fs = []
for o in [0,1,2]:
fs.append( lambda x: f(x,o) )

I'd expect that fs contains partial evaluated functions, i.e.

fs[0](0) == 0
fs[1](0) == 1
fs[2](0) == 2

But this is not the case :(

What is happening here?
Nevertheless, this code does work

fs = [ eval("lambda x: f(x,%d)" % o) for o in [0,1,2,3]]

Thanks.

Juan Pablo
Oct 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


You are asking it to return a list of lambda, not its evaluated value.

map(lambda x: f(x,0), [0,1,2]) works.

[ f(o) for o in [0,1,2] ] works too.

Juan Pablo Romero wrote:
Hello!

given the definition

def f(a,b): return a+b

With this code:

fs = [ lambda x: f(x,o) for o in [0,1,2]]

or this

fs = []
for o in [0,1,2]:
fs.append( lambda x: f(x,o) )

I'd expect that fs contains partial evaluated functions, i.e.

fs[0](0) == 0
fs[1](0) == 1
fs[2](0) == 2

But this is not the case :(

What is happening here?
Nevertheless, this code does work

fs = [ eval("lambda x: f(x,%d)" % o) for o in [0,1,2,3]]

Thanks.

Juan Pablo


Oct 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.