September 26th, 2003, 12:46 AM
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, 01:55 AM
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:
If you use lambda expressions, there's no need to declare a separate function. Simply declare a lambda expression within the map function:
return x * x
list = [1, 2, 3, 4]
list2 = map(square, list)
list = [1, 2, 3, 4]
list2 = map(lambda x: x * x, list)
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
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
September 26th, 2003, 04:16 AM
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 syntax thingy x % 2 != 0 amd x % 3 != 0)
>>> 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
Last edited by netytan; September 26th, 2003 at 04:19 AM.
September 26th, 2003, 09:30 AM
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, 10:42 PM
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, 11:12 PM
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
September 26th, 2003, 11:51 PM
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.