skeledrew 2 days ago

Interesting. I see HN has mangled my code block onto 1 line and replaced a couple stars, but the fixup should be obvious...

Yes, it is, it should, and that's exactly the point. It'd look janky even without the `[-1]`, and this is what the core devs are trying to protect against. Lambdas are anonymous functions meant only to be used in places where expressions are valid, such as function parameters and return values. There's even a linter warning if you assign one to a variable. All to help reduce the creation of janky-looking code, and that's a huge benefit for most developers.

  • greener_grass 2 days ago

    But what if I want to use lambdas for more things?

    Imperative programming only gets you so far.

    Maybe this is a sign that people are using Python for grander things than it was designed for?

    • skeledrew 2 days ago

      I feel like there is something missing here. What's stopping you from using a normal def? Aside from the definition itself not being usable inline, there is nothing lambda does that def doesn't. And if you really want a definition close to the calling site, just define it there and then put the name where you want to pass it.

      At the end of the day though there's really nothing to prevent you from creating janky code. Heck I saw a wild hack a couple weeks ago that allows for the creation of arbitrary custom syntax with pure Python, so you could create a multi-line lambda if you really want to that badly. But the widely adopted conventions exist for a reason.

      • greener_grass 2 days ago

        > Aside from the definition itself not being usable inline, there is nothing lambda does that def doesn't

        Being unable to position it inline is the problem.

        You might not see the benefit, but many do. It prevents Python from being a good functional programming, for one thing.

        • skeledrew 2 days ago

          There's 0 problem with doing:

              def outer(a):
                  def inner(b):
                      return a * b
                  return inner
          
          Though slightly longer, for most developers, it's still more grokkable (and related stack traces better) than:

              def outer(a):
                  return lambda b: a * b
          
          A very large part of Python's design is to emphasize readability for the majority. I still remember how long it took me to wrap my head around this "lambda thing" that I'd see pop up ever so often, even after a couple years of using Python. I eventually got fed up and took some time to really get to understand it. This shouldn't have to be the case for everyone reading random code.

          Python is a primarily OOP-based language with functional aspects. And the design decisions that went into it are what makes it so popular today. It's not Haskell or Lisp or any of the other many that the majority avoid due to language complexity. Don't try to make it into one.