Python Forum
Lambda Functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Lambda Functions (/thread-191.html)

Pages: 1 2


Lambda Functions - ATXpython - Sep-28-2016

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!


RE: Lambda Functions - micseydel - Sep-28-2016

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.


RE: Lambda Functions - ATXpython - Sep-29-2016

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.


RE: Lambda Functions - metulburr - Sep-29-2016

Here is another example we had on the tutorials
http://www.python-forum.org/viewtopic.php?f=25&t=7673


RE: Lambda Functions - nilamo - Sep-29-2016

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.


RE: Lambda Functions - j.crater - Sep-29-2016

(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?


RE: Lambda Functions - micseydel - Sep-29-2016

(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.


RE: Lambda Functions - nilamo - Sep-29-2016

(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.


RE: Lambda Functions - j.crater - Sep-29-2016

(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


RE: Lambda Functions - nilamo - Sep-29-2016

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.