473,662 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1711
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(squa re, 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(lamb da 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********@NOS PAMquick.cz> wrote in message news:<cg******* ***@ns.felk.cvu t.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
3668
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 are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
63
3373
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 suddenly become harder than they need to be. An example of what I mean is a quick script I wrote for doing certain actions based on a regexp, which I will simlify in this instance to make the pertanant points more relevent.
26
3482
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 of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr> usage) These are lambdas used when a lambda wasn't needed -- an anonymous function was created with...
181
8779
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 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
25
2556
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 run-time: someobject.newfunc = lambda x: f(foo, x) The reason a nested function doesn't work for this is that it is, well, dynamic. I don't know how many times or with what foo's this will be done.
4
2622
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 other mathematical symbols. In my mind, i imagine that those obscure math
23
5308
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 I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
5
2156
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 bit complicated so any explanation would be appreciated. Thanks
21
1841
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 following function? def addn(n): return lambda x,inc=n: x+inc if i do addn(5) it returns
1
2623
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 boost::lambda::lambda_ functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.