Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   lambda ?? (http://www.velocityreviews.com/forums/t335099-lambda.html)

Michael Foord 08-27-2004 08:00 AM

lambda ??
 
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

Benjamin Niemann 08-27-2004 08:17 AM

Re: lambda ??
 
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


Paul Rubin 08-27-2004 08:21 AM

Re: lambda ??
 
fuzzyman@gmail.com (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.

Jan Gregor 08-27-2004 09:09 AM

Re: lambda ??
 
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 <fuzzyman@gmail.com> 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


Michael Foord 08-28-2004 07:51 AM

Re: lambda ??
 
Jan Gregor <gregor.jan@NOSPAMquick.cz> wrote in message news:<cgmtp7$f6v$1@ns.felk.cvut.cz>...
> 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


All times are GMT. The time now is 07:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.