473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about what lamda does

Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for. What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.

thanks for any clarity

sk

Jul 18 '06 #1
16 1567
ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for.
It defines a function.

f = lambda x, y: expression

is equivalent to

def f(x, y):
return expression

Note that lambda is an expression while def is a statement.
What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.
Lambdas are typically used as parameters to functions that take
functions as arguments, like property() and reduce(). You never *need*
to use one, but sometimes it's convenient.

Jul 18 '06 #2
ok, i think i get it.
pretty cool.
thanks
-sk
Dan Bishop wrote:
ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for.

It defines a function.

f = lambda x, y: expression

is equivalent to

def f(x, y):
return expression

Note that lambda is an expression while def is a statement.
What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.

Lambdas are typically used as parameters to functions that take
functions as arguments, like property() and reduce(). You never *need*
to use one, but sometimes it's convenient.
Jul 18 '06 #3
Use it anywhere a quick definition of a function is needed that can be
written as an expression. For example when a callback function is
needed you could say:
def callback(x,y):
return x*y
some_function(w hen_done_call_t his=callback)
But with lambda you could just write
some_function(w hen_done_call_t his=lambda x,y:x*y)
Note: because it is an _expression_ you cannot do stuff like 'if..else'
inside of lambda.

-Nick V.

ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for. What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.

thanks for any clarity

sk
Jul 18 '06 #4
so a lamda needs to stay at one expression, and use more than one lamda
for more expressions ?

i think i get it.

sk

Nick Vatamaniuc wrote:
Use it anywhere a quick definition of a function is needed that can be
written as an expression. For example when a callback function is
needed you could say:
def callback(x,y):
return x*y
some_function(w hen_done_call_t his=callback)
But with lambda you could just write
some_function(w hen_done_call_t his=lambda x,y:x*y)
Note: because it is an _expression_ you cannot do stuff like 'if..else'
inside of lambda.

-Nick V.

ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for. What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.

thanks for any clarity

sk
Jul 18 '06 #5
The two primary differences between using def and using lambda is that
lambda is limited to a single expression and def cannot be used within
another function.

Basically, use lambda when you need to define a small function within
another function. I've also used it to create 'shortcut' functions as:

bar = lambda a, b: object1.object2 .function1( a ).function2( b )
foo = bar( somevalue1, somevalue2 )

It can save you a lot of typing if used wisely, and makes your code
smaller and neater too :)

On a real world example:

import random
roll_die = lambda sides=6: random.choice( range(1,sides+1 ) )
# roll dies with 4, 6, and 20 sides
print roll_die(4), roll_die(), roll_die(20)
Have fun with your lambdas.

greb

ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for. What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.

thanks for any clarity

sk
Jul 18 '06 #6
gr******@gmail. com wrote:
The two primary differences between using def and using lambda is that
lambda is limited to a single expression and def cannot be used within
another function.
'def' can certainly be used within another function :

def make_adder( delta ) :
def adder( x ) :
return x + delta
return adder

[sreeram;]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEvQNjrgn 0plK5qqURAjQxAK CMz0WV/0ZAfW/en4IVHGMztxzWdQ CgrYWH
rOS510r1w1GTDB+ 5Nqj1cFQ=
=ISgg
-----END PGP SIGNATURE-----

Jul 18 '06 #7
gr******@gmail. com wrote:
The two primary differences between using def and using lambda is that
lambda is limited to a single expression and def cannot be used within
another function.
Where on earth did you get that from? I presume you mean "You can't use
a def statement as an argument to a function"?

There is nothing wrong with

def adder(x):
def func(y):
return x+y
return func

for example. Then adder(5) returns a function that returns 5 more than
its argument.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 18 '06 #8
ne*****@xit.net wrote:
Hey there,
i have been learning python for the past few months, but i can seem to
get what exactly a lamda is for. What would i use a lamda for that i
could not or would not use a def for ? Is there a notable difference ?
I only ask because i see it in code samples on the internet and in
books.
Lambda is just as powerful as a function, but totally useless =-P

Lambda used to be handy before the introduction of list comprehensions.
Now, though, there primary use is obfuscating your code.

Jul 18 '06 #9
I stand corrected. Not sure where I got that from, improper
defragmentation due to sleep depravation perhaps...

K.S.Sreeram wrote:
gr******@gmail. com wrote:
The two primary differences between using def and using lambda is that
lambda is limited to a single expression and def cannot be used within
another function.

'def' can certainly be used within another function :

def make_adder( delta ) :
def adder( x ) :
return x + delta
return adder

[sreeram;]
--------------enigFDB411206B5 4B101CC680F5A
Content-Type: application/pgp-signature
Content-Disposition: inline;
filename="signa ture.asc"
Content-Description: OpenPGP digital signature
X-Google-AttachSize: 253
Jul 18 '06 #10

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

Similar topics

21
2378
by: j_mckitrick | last post by:
Just thought I'd play devil's advocate... I tried wxPython briefly, and it had some nice features. It has a huge list of dependencies. I don't like the fonts (but I'd love to know how to get fixed fonts, or at least anything that looks better on a small laptop screen. TkInter seems much easier to use... less effort to do things.
65
5394
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++ language. A C++ interface installation IS ABOUT THE C++ LANGUAGE! The language does not possess the ability to handle even simple file directory manipulation. Those wise people that created it did not take care of it. So, BOOST is a portable...
9
3752
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? Here is an example of just such a usage: <dl class="faq"> <di>
11
4270
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
29
3579
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
53
4095
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
1452
by: Bhuwan Bhaskar | last post by:
Hi, I want to know what is anonymas methods and how they can be used. Thanks and regards. Bhuwan
3
1331
by: gnewsgroup | last post by:
I want to get my hands dirty with Linq, so I watched a few videos from www.asp.net, and then turned to http://msdn2.microsoft.com/en-us/library/bb397687.aspx for some introduction about the new lamda feature of C#. In the article linked above, there is an example reproduced here: int numbers = {5,4,1,3,9,8,6,7,2,0 };
5
2326
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for explaining a "lamda" expression and so I don't know what one is and I am not doing any database stuff in the entire program. So what does this error message really mean and what can I do to get an On Error into the routine?
0
9647
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
9496
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
10363
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
10164
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...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6745
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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

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.