+ 2

why use lambdas functions?

Wouldn't this be more simple: x = -4 print(x**2 + 5*x + 4) instead of this: print((lambda x: x**2 + 5*x + 4) (-4))

9th Jul 2016, 4:57 AM
lightmaster
lightmaster - avatar
2 Antworten
+ 4
Lambdas are useful when you want to pass some action to execute to another function. Typical cases are filter() and map(), they apply some action to the elements of a list, in order to realise a transformation of that list. They abstract the transformation and this way they can be used for any particular transformation. Lambdas here are a very practical way to define a particular action in the same line you invoke the outer function, without having to define a whole new function for it. This is a significant case for lambdas, because usually actions for filters and mappings are trivial bits of code and writing them inline is very useful.
9th Jul 2016, 9:49 AM
Zak Mc Kracken
Zak Mc Kracken - avatar
+ 1
lambda function are used when there is an single line expression to return from a function,and with map and filter it is precise way of writing functions and extracting values out of iterables.
9th Jul 2016, 9:25 AM
jayrajarora2694
jayrajarora2694 - avatar