+ 6

Why a lambda function would ever be more useful than a normal function.. Can somebody explain?

17th Nov 2015, 12:28 PM
Max Werman
Max Werman - avatar
10 Respuestas
+ 13
sometimes you might not need to pack your code with name defined functions. Let's imagine that you want to have the age of a user stored in months for precision in some process. But when the user access his profile you want him to see it in years. You can quickly define a lambda function to define his age in years. But you probably don't want it to be modified (a year will always be 12 months). You won't need to use it more than once so you don't need to pack your code with unnecessary functions.
15th Jan 2016, 6:01 PM
Tiago Paim
Tiago Paim - avatar
+ 6
the main reason is to minimise the length of the code...instead of making a whole new function like def func(arg): (this is a named function) expression return and then print func(arg) you can use print((lambda x: expression )(arg)) It will take less space n time and u don't have to make a whole function so it will be quick...but it can not be used for expressions which are more than one line ex. loops or if else.
28th Jan 2016, 5:21 PM
Gaurav Singh
Gaurav Singh - avatar
+ 5
Lambda functions are most useful when you need to supply a simple function as a parameter to another function. For example there is a function map() which modifies every item of a list using a supplied fuction to create a new modified list. Let's say you want to increment all items in a list. You would supply "lambda x: x + 1" as a parameter: map(lambda x: x + 1, my_list) Defining x + 1 as a named function elsewhere in the program would not be practical. It would look like this: def increment(x): return x + 1 map(increment, my_list) The code is much more talkative, separated to multiple places and in the place of calling map() you do not see what does increment() exactly do. Similarly as "3" is a number literal (compared to a variable which could contain the number) "lambda x: x + 1" is a function literal (contrary to a named function like increment() above). As you would not always write "variable = 3" and then write "variable" instead of "3" when you want to use the number 3 in your program you would not define a simple function when you can use a lambda directly in place instead.
28th Jun 2016, 7:49 PM
Václav Brožík
Václav Brožík - avatar
+ 2
I believe you would prefer to use lambdas on situations where you need a function that is simple and you won't need it again later on in your code. So by using a lambda, you just execute it on the fly instead of defining a function, and then calling, executing and returning a value from it.
28th Feb 2016, 5:55 PM
Alex Munaretto
+ 2
cause it reduces coding. I mean less code same purpose. more effective
8th Dec 2016, 9:44 PM
Hetbo.net
Hetbo.net - avatar
+ 1
If u don't need to reuse functions, always lambda is a good option, otherwise u can implement a function with a lot of function inside, very practical really
21st Feb 2016, 10:21 PM
Emanouela Stoianova
Emanouela Stoianova - avatar
+ 1
I wondered the same thing. After a quick search I found that David Z (not me) has a great response here: http://stackoverflow.com/questions/890128/why-are-JUMP_LINK__&&__python__&&__JUMP_LINK-lambdas-useful You can see from his last example that it allows you to create a function that returns a function. I think this may be THE BEST reason to use it. Just make sure that you couldn't have applied list comprehension or some other more readable technique instead. Other reasons: #1: It makes you feel powerful to know how to use something that most people don't. #2: ...No one will like you for using it if you only do so to experience reason #1. The best thing with coding is have more knowledge and consider different angles to attack a problem. You may find a use for it yet. \
16th Jun 2016, 3:30 PM
David McNamara
+ 1
in this case you are right but with a little creativity you can use it better. Ill write a code and you will see what I mean. just remind me to do it cause im busy now.
19th Nov 2016, 2:42 PM
Hetbo.net
Hetbo.net - avatar
+ 1
This will impact the complexity of your program ;).. A lambda function can pass out a function with loop or other things... I think, no?
27th Dec 2016, 11:15 AM
A.M
A.M - avatar
0
Lambda expressions are extremely advantageous for parallelization of operations !!!
15th Feb 2016, 3:55 AM
Shiban Shaikh Qureshi
Shiban Shaikh Qureshi - avatar