Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why does it need to be anonymous if it's multi line? You can just do a local 'def'.


You cannot use def within function call parenthesis.


But you can pass a def'd function within function call parentheses.


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


Yes, 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)


In 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`


I suppose lambdas are already a bit weird for ending with a : without starting a new code block.

Good luck fixing that though.


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




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

Search: