+ 6
Why a lambda function would ever be more useful than a normal function.. Can somebody explain?
10 odpowiedzi
+ 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.
+ 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.
+ 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.
+ 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.
+ 2
cause it reduces coding. I mean less code same purpose. more effective
+ 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
+ 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.
\
+ 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.
+ 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?
0
Lambda expressions are extremely advantageous for parallelization of operations !!!