Write complicated anonymous functions other than lambdas in Python
github.com
github.com
def _(a, b):
# a multi-lined function
return a + b
high_order_function(_)
Wouldn't this achieve exactly the same thing, in basically the same line count?Or better, give the function a simple name.
Many times I've found myself wanting to write a function that I only need to use once. APIs for example, when they require a function argument. Or even just when making a "list comprehension", maybe I need more than a 1-liner lambda. I really miss this in Python. Having said that, I won't use the implementation given in this post because it just looks overly complex.
Then write a function that you use once. Give it a short name, or just call it f. You could even give it a leading underscore, and that will signal that the function is private to that module and it won’t get imported on * imports.
Naming things is a burden, one of the two hardest computer science problems. Mandating that all functions be named is like mandating that all values be named - no "x * 2" for you! You have to do "doubling_constant = 2: x * doubling_constant". It's true that beyond a certain level of complexity, a name can help convey intent - but that's a decision best left to the programmer, not enforced by some stupid arbitrary parsing rule.
> Lambdex appears to be less readable than functions and will mess up my code. Why should I use it?
> The project is not to criticize the present design of Python, but an experimental attempt to provide alternative for the ones who need a better anonymous function expression. The need may be from a second language they are familiar with, or the paradigms they want to use.