Hacker News new | past | comments | ask | show | jobs | submit login
Write complicated anonymous functions other than lambdas in Python (github.com/hsfzxjy)
22 points by satya71 on March 4, 2021 | hide | past | favorite | 16 comments



Why 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.


Yep, so much simpler and easier to read.


This 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.


> 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.


> 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.


And 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.


You 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.


Call them all _f. If you have a linter it may need tweaking.


That's an impressive amount of work to make your code harder to read!


God 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.


"Lambdexes use < instead of = for assignments" !


I would probably be grumpy if I encountered this code during a work day.


I’ll never use this. But the ingenuity to bend Python to the author’s will is admirable.


why


Same reason as code golf or IOCCC I guess. To show that it can be done.


These 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: