473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fun with lambdas

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
1 1312


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michal | last post by:
Hi, Today I started with python and I cannot understand why expresion lambda x: print x returns syntax error (but for example lambda x: sys.stdio.write(x) does not) First I fought the reason is...
4
by: gong | last post by:
hi i would like to pickle a lambda; according to the library docs in 2.3, i believe this shouldnt be possible, since a lambda is not a function defined at the top level of a module (?) ...
6
by: John Fouhy | last post by:
So I'm trying to generate Tkinter callback functions on the fly, but it's not working, and I don't understand what's going on. Here is an example program: -------------------------- from...
2
by: Fernando Perez | last post by:
Hi all, there are a couple of threads on lambdas today, which got me curious about their differences as far as bytecode goes: planck|2> lf=lambda x: x**2 planck|3> def ff(x): return x**2 |.>...
51
by: bearophileHUGS | last post by:
On Slashdot there is a discussion about the future C#3.0: http://developers.slashdot.org/developers/05/09/18/0545217.shtml?tid=109&tid=8 http://msdn.microsoft.com/vcsharp/future/ There are...
5
by: Chris Johnson | last post by:
What I want to do is build an array of lambda functions, like so: a = (This is just a demonstrative dummy array. I don't need better ways to achieve the above functionality.) print ...
29
by: jmDesktop | last post by:
For students 9th - 12th grade, with at least Algebra I. Do you think Python is a good first programming language for someone with zero programming experience? Using Linux and Python for first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.