Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lambda Functions
#1
Hello,

I've been studying Python 3 for a few weeks now, and have seen some people using Lambda Functions.

From what I understand, these are nameless (or anonymous) functions.
But what I don't understand, is why/when you would use these.

Some help for a noob would be greatly appreciated!
Reply
#2
They're great when you want to pass something callable without going to the effort to create and name a function. Are you familiar with defaultdict? Takes this example, which shows how to get a defaultdict to give you a 1 instead of a 0

>>> from collections import defaultdict
>>> defaultdict(int)['does not exist']
0
>>> defaultdict(lambda: 1)['does not exist']
1
Lambdas are also useful in map() calls, though Python's comprehensions make that generally less common.
Reply
#3
Thanks for taking the time to reply.
Im not familiar with the defaultdict feature that you mentioned.
Ill do some additional research - seems like this is beyond my level at the moment.
Reply
#4
Here is another example we had on the tutorials
http://www.python-forum.org/viewtopic.php?f=25&t=7673
Recommended Tutorials:
Reply
#5
It's useful for times when you need some sort of a callable parameter, but you don't actually care about what the param is, or you only need to do something very simple and creating a named function is not needed.  Like event handlers.  Here's some pseudocode to illustrate:
MyButton.onclick(lambda: print('testing'))
In python, lambdas are very limited to the point of being almost completely worthless (only one line, can't have multiple statements, can't assign to variables, etc).  In other languages (such as ruby or javascript), they are used very, very extensively in almost every package.  Because they're so limited in python, you could just never use them, and not be missing anything important.
Reply
#6
(Sep-29-2016, 03:29 PM)nilamo Wrote: Because they're so limited in python, you could just never use them, and not be missing anything important.

Regarding this part... do you (or anyone else) know if this is likely to change in future? Or perhaps there is even ongoing effort in that direction?
Reply
#7
(Sep-29-2016, 05:58 PM)j.crater Wrote:
(Sep-29-2016, 03:29 PM)nilamo Wrote: Because they're so limited in python, you could just never use them, and not be missing anything important.

Regarding this part... do you (or anyone else) know if this is likely to change in future? Or perhaps there is even ongoing effort in that direction?

What do you mean? They won't be removed from the language. Just don't worry about them until they come up, since you aren't missing much. If you find yourself passing functions as arguments, and you want shorter syntax, that's essentially all they do.
Reply
#8
(Sep-29-2016, 05:58 PM)j.crater Wrote:
(Sep-29-2016, 03:29 PM)nilamo Wrote: Because they're so limited in python, you could just never use them, and not be missing anything important.

Regarding this part... do you (or anyone else) know if this is likely to change in future? Or perhaps there is even ongoing effort in that direction?

I think I read once that most of the devs working on adding features to python don't like lambdas, as their existence runs in the opposite direction to the cleanliness and legibility of the entire rest of the language.
Reply
#9
(Sep-29-2016, 06:00 PM)micseydel Wrote: What do you mean? They won't be removed from the language. Just don't worry about them until they come up, since you aren't missing much. If you find yourself passing functions as arguments, and you want shorter syntax, that's essentially all they do.

Sorry for being unclear. I was thinking if they plan to expand functionality, to make lambdas more useful, or so. Removing lambdas is also something they could do, but it really didn't come across my mind :D
Reply
#10
They won't be removed or expanded. They're designed only for to be one line of code that doesn't assign values to anything. If you need more than what they do, you need to actually declare a function.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple lambda functions in zipped list not executing psolar 0 1,603 Feb-13-2020, 12:53 PM
Last Post: psolar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020