3 ms·
Why does it need to be anonymous if it's multi line? You can just do a local 'def'.
by klipt 6y ago
Why does it need to be anonymous if it's multi line? You can just do a local 'def'.
- rowanG077 6y agoYou cannot use def within function call parenthesis.
- OJFord 6y agoBut you can pass a def'd function within function call parentheses.
- rowanG077 6y agoOf course you can. But it's very different. A lambda is guaranteed to only be used by the function it's passed to. A local def pollutes the scope with an additional identifier that leads to less code clarity.
- gnulinux 6y agoI would still prefer having a multiline lambda. We have this: def f(x): y = foo(x) z = bar(y) // ... return x some_obj.do_later(t, f, a, b, c) I would prefer: some_obj.do_later(t, lambda x: { y = foo(x) z = bar(y) // ... return x }, a, b, c) Maybe ( instead of {? I'm not sure that's possible to parse. Regardless, something like this would be good.
- OJFord 6y agoYes, I have wanted that before, and it could use existing valid syntax: some_obj.do_later(t, (lambda x: # (presently only a single non-comment line would be valid) x ), a, b, c)
- deleted 6y ago[deleted]
- contravariant 6y agoI suppose lambdas are already a bit weird for ending with a : without starting a new code block. Good luck fixing that though.
- cwhy 6y agoIn Python people are encouraged to follow styles for code as a communication tool between developers. If you just really want your code to "look nice" you can have things like this: def begin(*args): return args[-1] begin( func := lambda x, y: begin( z := int(input()), x + y + z ), func(1, 2) ) from `https://news.ycombinator.com/item?id=23346534 https://news.ycombinator.com/item?id=23346534`