5 ms·
Write complicated anonymous functions other than lambdas in Python
- n0b0dy_ 6y agoThis looks like an interesting implementation to solve a non-existing problem. AFAIK anonymity is not as much as a feature as a consequence of the design of lambdas. If one needs to use complex operations, creating a normal function (with a generic name) is the way to go.
- rambojazz 6y ago> a non-existing problem 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.
- travisjungroth 6y ago> Many times I've found myself wanting to write a function that I only need to use once. 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.
- dTal 6y agoAnd if I have many single-use functions in a module? I call them _f1(), _f2(), _f3() etc? 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.
- travisjungroth 6y agoCall them all _f. If you have a linter it may need tweaking.
- powersnail 6y agoYou don't have to define them in a module. Just define them on the spot, like a lambda. So all of them can be named `f` without a problem.
- powersnail 6y agoWhy not just: 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.
- greenie_beans 6y agoYep, so much simpler and easier to read.
- IshKebab 6y agoThat's an impressive amount of work to make your code harder to read!
- AllegedAlec 6y agoGod I agree with this so much. Why would you even want to get rid of the metadata of function headers to help you quickly parse and understand code.
- yesenadam 6y ago"Lambdexes use < instead of = for assignments" !
- greenie_beans 6y agoI would probably be grumpy if I encountered this code during a work day.
- wshm 6y agowhy
- satya71 6y agoSame reason as code golf or IOCCC I guess. To show that it can be done.
- sli 6y agoThese projects always have their rationale in their README. This one isn't an exception: > 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.
- satya71 6y agoI’ll never use this. But the ingenuity to bend Python to the author’s will is admirable.