The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Let's get those lambdas straight
Discuss Let's get those lambdas straight in the Python Programming forum on Dev Shed. Let's get those lambdas straight Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 25th, 2003, 11:46 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
|
Let's get those lambdas straight
Ok, now, I've tried my best to get familiar with them, but I just can't understand what the HECK a lambda does. I've looked into every book I could manage to find, the Python tutorial...
So ? Help me please. I feel like a Ph.D. in math who can't add two numbers. Kinda.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz
|

September 26th, 2003, 12:55 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
A lambda is something python borrowed from functional programming languages. Basically, a lambda function is sort of like a regular function, except it does not have a name. Actually, it is more correct to call it as a lambda expression rather than a function. This means, a lambda can appear in the middle of a statement unlike a def statement.
So where would you use this? Well, it comes in handy, if you're planning to write quick event handlers and don't want to declare a separate function. Another classic use is when you have a list of items and want to apply a transform to all items in the list. For example, let's say you want to square all items in a list. The non-lamdba method would be to declare a function (we call it square()) and pass it as one of the arguments to the map() function, something like this:
Code:
def square(x):
return x * x
list = [1, 2, 3, 4]
list2 = map(square, list)
print list2
If you use lambda expressions, there's no need to declare a separate function. Simply declare a lambda expression within the map function:
Code:
list = [1, 2, 3, 4]
list2 = map(lambda x: x * x, list)
print list2
HTH 
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

September 26th, 2003, 03:16 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Sweet explination scorpy,  damn sure i couldn't have done any better! Anyway, a simple example which uses filter() and lambda to return all the odd and even numbers in a list..
>>> l = range(50)
>>> filter(lambda x: x % 2, l)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
>>> filter(lambda x: not x % 2, l)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>>
and another which returns prime numbers up to 25 (I've borrowed the syntax from http://forums.devshed.com/showthrea...&threadid=68936)
>>> filter(lambda x: x % 2 != 0 and x % 3 != 0 and x < 25, l)
[1, 5, 7, 11, 13, 17, 19, 23]
>>>
IMO they can be useful and save you a lil time and space  but kinda limited and not something i've really used to much, prob because i don't write many one liners
have fun,
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : September 26th, 2003 at 03:19 AM.
|

September 26th, 2003, 08:30 AM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
Actually, lambda's are function makers to be more correct. The lambda keyword creates an anonymous function.
Aside from using them with other functional constructs like map and filter, you can use them to return functions from within functions (making your own function-making functions). You can do this with locally nested scopes as well, but sometimes using a lambda is just easier.
|

September 26th, 2003, 09:42 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
|
Well all those explanations were 10000% clearer than any book I'd read. Thanks a bunch, guys.
As of performance, how do lambdas perform versus, say, function calls ?
|

September 26th, 2003, 10:12 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Very welcome Solar! I don't think there's much differance preformace wise, you might find http://www.szgti.bmf.hu/harp/python/fastpython.html interesting though
I'd guess that lambda's would be a lil more efficent since there really just spiced up expressions which get called once then discarded, though they can only be pretty simple..  it really depends on what you want to use them for, but they have nothing on functions  IMO
Mark.
|

September 26th, 2003, 10:51 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
Quote: Originally posted by SolarBear
As of performance, how do lambdas perform versus, say, function calls ? |
It should be identical. As far as I know, lambda-made functions and regularly def-made functions are compositionally identical, it's just that one is bound to a name and one isn't.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|