DEV Community

Discussion on: what are lambda functions in python

Collapse
 
val_baca profile image
Valentin Baca

What restrictions? How are they are random?

Python may not be functional but it does support functions as a first class object.

They can be hard to read but anything can be written to be hard to read.

They are incredibly useful in list comprehensions.

Collapse
 
andrewlucker profile image
Andrew Lucker • Edited

Python uses whitespace indentation for blocks like for loops etc. This is context-sensitive, not context-free grammar. For this reason Lambda bodies can't use blocks, only expressions.

In Python 2, lambda lhs syntax was a bit borked but that is fixed in 3. Also the lambda object behaved slightly differently than a normal function. This is of course assuming that you are trying to do something stupid like unwind the stack, otherwise I don't think you would normally notice the difference.

The latter part is what I would call random, but mostly is not applicable to Python 3 anymore. However, to get there Python abandoned some simple conveniences like the very minimal destructuring that 2 did support for function arguments: lambda a,(b,c): a+b+c

Python does not have poor ergonomics by any means, I just wouldn't recommend Functional style because it looks horrible and isn't well supported.

As for list comprehensions, actual list/iter comprehension look better than map/filter imho. Also this is the preferred syntax in uber-functional languages like Haskell.