473,395 Members | 1,761 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,395 software developers and data experts.

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
Jul 18 '05 #1
4 1696
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

Jul 18 '05 #2
fu******@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.
Jul 18 '05 #3
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 <fu******@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

Jul 18 '05 #4
Jan Gregor <gr********@NOSPAMquick.cz> wrote in message news:<cg**********@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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.