some_function = lambda x: 10 if x == 6 else 1
- syntactic sugar for:
def some_function(x): return 10 if x == 6 else 1
The value is that it will return 10 if x == 6 evaluates to True and returns 1 otherwise.
Personally, I prefer the def form in all but the simplest cases, since it allows multi-line functions, makes it clearer what overhead is associated with calling the callee, simplifies the analysis of closing the function, and opens the mind of the new python programmer to other, more complex code objects (such like classes) that can be just as easily created at runtime.
source share