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

Someone can finally implement multiline lambdas!


Python DOES have multiline lambdas! It's a common misconception that lambdas have to all be on one line. They don't. What lambda's can't have are statements. Lambdas are restricted to a single expression due to their implicit return statement, but that expression can have any number of line breaks in it. I still shake my head when I hear this complaint about Python, because when coding in the functional style (which is when you need lambdas), expressions are all you need! Haskell and friends don't even have statements, just expressions. My Drython experiment (though unpythonic) demonstrates just how far you can stretch Python's lambdas. Check out its readme for examples: https://github.com/gilch/drython

Hissp, my Lisp-on-Python project, uses multiline lambdas to simplify its compilation target to a functional subset of Python: https://github.com/gilch/hissp

Hebigo is a skin built on Hissp with a more Python-like syntax and macros based on Drython. It's what Python would be like if its statements were composable like its expressions are: https://github.com/gilch/hebigo


Is there some way of simulating expression-based "let" in Python? Without that lambdas are very weak.


Recall that Python has assignment expressions `:=` now: https://www.python.org/dev/peps/pep-0572/

Even before that, I had a "let" in Drython: https://github.com/gilch/drython/blob/master/drython/stateme...

In Haskell let and lambda have a subtle distinction due to the type system, but in Lisp and dynamically typed languages like Python, "let" can simply be lambda definition that is called immediately.


You can actually just use a regular function declaration in the middle of arbitrary Python code if you want. Does that count as a multi-line lambda?


No.


not trying to shitpost - I'm legit curious. What's the difference between the two? Like in this snippet, are the two resulting functions different from each other in some way that I haven't considered?

    def parent_function():
        output_function_a = lambda x,y: (x + 23) * y

        def output_function_b(x, y):
            return (x + 23) * y 
        
        return output_function_a, output_function_b


The true comparison is

    def parent_function():
        def output_function_b(x, y):
            return (x + 23) * y 
        
        return lambda x,y: (x + 23) * y, output_function_b


Python lambdas don't have names, and can be defined in expression context (as in, 'map(lambda foo: bar, baz)').




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: