I am learning Python since one year, I don't found 'lambda' useful. 'lambda' is much similar to 'def' statement. I need an example where I must need to use 'lambda' statement and I can't use 'def' here at all.
Asked
Active
Viewed 72 times
0
-
`lambda` is simply for convenience. It is never necessary. – zondo May 20 '16 at 00:34
-
you must use the lambda function if you are only allowed to use one line to write the function ... – Joran Beasley May 20 '16 at 00:35
-
`a.sort(lambda x: x.foo)` vs. `def callback(x): return x.foo; a.sort(callback)` – use it when it makes sense, that's all. – deceze May 20 '16 at 00:37
-
http://stackoverflow.com/a/12264881/524743 – Samuel May 20 '16 at 00:41
-
1lambda is not a statement, it's an expression. That's why you can use it in expression contexts. – Felix Kling May 20 '16 at 00:43
-
It's like the different between `x + y` and `z = x + y`. The first calculates a value, the second calculates the value and gives it a name. – Barmar May 20 '16 at 00:52
-
As the very first comment says: **it is never _necessary_.** And I posted an example of where a `lambda` makes more sense because it is much more concise. There's nothing more to it than that. It's simply *more concise* when you don't need a full `def`. – deceze May 20 '16 at 01:15