News for hackers. Stuff that matters.
Re:"Real" anonymous functions for Python by wodenokoto on Friday March 29, 2024 @04:02AM (#39861640)
I didn’t quite understand what the “real” anonymous functions are compared to lambda. Is it just that the lambdas aren’t multilines?

Having grown up with Python lambdas, maybe I’m blind to their limitations, but I didn’t feel like there was any real argument as to why not make a real function when you need more than a lambda.

Re:"Real" anonymous functions for Python by ElevenLathe on Friday March 29, 2024 @10:10AM (#39864318)
The major killer feature of scheme-style lambdas is that you don't have to name them. Naming stuff is hard, and you usually get it wrong. If the thing you want to do is a couple lines of code, and you can just drop it in as a lambda and not have to think of a meaningful name, it's easier to keep flow-state. If things expand to the point where glancing at the code doesn't tell you what the function does, that is the time to break it out into a named function.
Re:"Real" anonymous functions for Python by jononor on Saturday March 30, 2024 @09:06PM (#39880361)
Just call it foo. Not a problem.
Re:"Real" anonymous functions for Python by knightoffaith on Sunday March 31, 2024 @01:58PM (#39886437)
You can still do anything you want with just Python lambdas technically speaking right? Like, Python lambdas can express any lambda calculus expression, so as long as you're not writing something with side effects, you can use an arbitrarily complex Python lambda. Though I guess the resulting code wouldn't exactly be Pythonic.
Re:"Real" anonymous functions for Python by powersnail on Sunday March 31, 2024 @02:44PM (#39886866)
Depends on what you mean by "technically".

Computationally, python lambda is fully capable of whatever a regular python function can do, including side-effects, (you can call foo.set_bar()).

The limit is syntactical: a lambda has to be one single expression. So, no assignment statement, no multiple lines, etc.

Re:"Real" anonymous functions for Python by constantcrying on Sunday March 31, 2024 @02:39PM (#39886809)
Language design is really about what you could do, but about how to efficiently express certain things.
Re:"Real" anonymous functions for Python by JadeNB on Monday April 01, 2024 @07:48PM (#39900791)
> Language design is really about what you could do, but about how to efficiently express certain things.

That's missing a "not", right (probably after "really")?

Re:"Real" anonymous functions for Python by ElevenLathe on Wednesday April 03, 2024 @11:19AM (#39918611)
Not the person you're replying to, but I assume they meant "rarely" instead of "really". I make this typo all the time myself!
Re:"Real" anonymous functions for Python by JadeNB on Thursday April 04, 2024 @10:35AM (#39930956)
That makes sense. Thanks!
Re:"Real" anonymous functions for Python by mpeg on Sunday March 31, 2024 @02:00PM (#39886457)
I tend to use inner functions (nested def) when I need something more than a simple lambda, but don't want to come up with a good name or pollute the class / module namespace

It works okay, but I do wish there was a better way

Re:"Real" anonymous functions for Python by jmholla on Sunday March 31, 2024 @04:27PM (#39887767)
> It works okay, but I do wish there was a better way

What's wrong with it?

Re:"Real" anonymous functions for Python by mpeg on Sunday March 31, 2024 @05:39PM (#39888280)
nothing really, probably just me, but it always felt a bit ugly to nest function definitions, plus it can separate the (often single-use) function from the place it is being used in
Re:"Real" anonymous functions for Python by uberman on Sunday March 31, 2024 @02:16PM (#39886604)
Can you set a breakpoint inside a lambda function without resorting to using multi line trickery using braces that essentially does what people say lambdas should not do?
Re:"Real" anonymous functions for Python by maxcoder4 on Sunday March 31, 2024 @02:22PM (#39886651)
Not the OP, but I program professionally in Python for the past 5 years and i have never set a breakpoint. Assuming they don't work in lambdas (sounds like a tooling issue), is this really such a big problem?
Re:"Real" anonymous functions for Python by constantcrying on Sunday March 31, 2024 @02:36PM (#39886783)
>Not the OP, but I program professionally in Python for the past 5 years and i have never set a breakpoint.

This means you are either one of the best or one of the worst programmers. People in the middle have bugs in their program and can use a debugger to find them.

>sounds like a tooling issue

The tool, in this case, being the interpreter. Which is exactly the point.

Re:"Real" anonymous functions for Python by quibono on Sunday March 31, 2024 @02:28PM (#39886711)
Depending on what you're doing this can be really annoying.
Re:"Real" anonymous functions for Python by wodenokoto on Monday April 01, 2024 @07:06AM (#39892802)
No, but I also can’t set one in the middle of a chain of methods.

In Python it’s quite common to have to refactor in order to debug.