lambda ??

M

Michael Foord

I'm starting to read through the developer works article on Functional
Programming - http://www-106.ibm.com/developerworks/library/l-prog.html

It starts by giving some basic examples using lambda. What I'm
wondering is what's the actual difference between these two forms ?

pr = lambda s:s
*and*
def pr(s):
return s

Both bind the name 'pr' to a function object that does the same thing
?? I know that lambda functions can only be a single expression......

Is it just a basic example (and so in this case there is no
difference).. or am I missing something. (What's the point of an
'anonymous' function... if you give a name to it !!).

Regards,

Fuzzy

http://www.voidspace.org.uk/atlatnibots/pythonutils.html
 
B

Benjamin Niemann

lambda returns the functions, thus can be used *in* an expression or
function call, when a callable is required. This is often a useful shortcut.
But according to another thread 'lambda' is one candidate for things
that are dropped in a future version of Python...
 
P

Paul Rubin

It starts by giving some basic examples using lambda. What I'm
wondering is what's the actual difference between these two forms ?

pr = lambda s:s
*and*
def pr(s):
return s

They're the same.
Is it just a basic example (and so in this case there is no
difference).. or am I missing something. (What's the point of an
'anonymous' function... if you give a name to it !!).

It's like an anonymous expression. Look at the statement

x = a + b * c

That adds the expression 'a' to the expression 'b * c'. If Python
didn't have anonymous expressions, you'd say something like

temp = b * c
x = a + temp

Anonymous just means you can use it as an intermediate result without
having to give it a name of its own.

Example:

def derivative(f, x): # find approximate value of f'(x)
h = .0001
return (f(x+h) - f(x)) / h

def square(x):
return x*x

print derivative(square, 3) # approximately 6

An anonymous function lets you do the same thing without having to
create a named function (like a temporary variable):

print derivative(lambda x: x*x, 3) # same thing

Using a lot of lambdas can be like using a lot of complicated, deeply
nested arithmetic expressions. You have to exercise some judgement to
keep your code readable. But there's a school of thought that says
lambda is a wart in Python and shouldn't be used. That's as silly as
saying you should never say "a + b * c" and instead name every
subexpression with a temp variable.
 
J

Jan Gregor

Python is little bit limited in lambda functions. In lisp I use lambda
functions in functionals (something like a map or reduce in python)
as a wrapper for functions with more than one argument.

Jan
 
M

Michael Foord

Jan Gregor said:
Python is little bit limited in lambda functions. In lisp I use lambda
functions in functionals (something like a map or reduce in python)
as a wrapper for functions with more than one argument.

Jan

Thanks to those that answered. Looks like I need to elarn Lisp to
getter a fuller understanding... right after I learn C.

Hmmm... unfortunately it looks like Python 2.2 or 2.3 broke the
'closure' examples from the Charming Python 'Functional Programming'
series. Looks like the Xoltar toolkit could do with an update. The
article says that the nested scope rules of python 2.1 + mean that you
don't need the toolkit to do closures.... but without the examples
it's a little harder to follow. Maybe it's time for an update !!
Anyway - I did get some interesting ideas.

Regards,

Fuzzy

[snip...]

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top