Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > lambda ??

Reply
Thread Tools

lambda ??

 
 
Michael Foord
Guest
Posts: n/a
 
      08-27-2004
I'm starting to read through the developer works article on Functional
Programming - http://www-106.ibm.com/developerwork...ry/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/atlatnib...thonutils.html
 
Reply With Quote
 
 
 
 
Benjamin Niemann
Guest
Posts: n/a
 
      08-27-2004
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...

Michael Foord wrote:
> I'm starting to read through the developer works article on Functional
> Programming - http://www-106.ibm.com/developerwork...ry/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/atlatnib...thonutils.html

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      08-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Michael Foord) writes:

> 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.
 
Reply With Quote
 
Jan Gregor
Guest
Posts: n/a
 
      08-27-2004
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

On 2004-08-27, Michael Foord <(E-Mail Removed)> wrote:
> I'm starting to read through the developer works article on Functional
> Programming - http://www-106.ibm.com/developerwork...ry/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/atlatnib...thonutils.html

 
Reply With Quote
 
Michael Foord
Guest
Posts: n/a
 
      08-28-2004
Jan Gregor <(E-Mail Removed)> wrote in message news:<cgmtp7$f6v$(E-Mail Removed)>...
> 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/atlantib...thonutils.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
A critic of Guido's blog on Python's lambda Xah Lee Java 177 05-23-2006 07:11 PM
A Lambda Logo Tour Xah Lee Java 5 04-07-2006 04:07 PM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments