For further actions, you may consider blocking this person and/or reporting abuse
Read next
๐Whatโs New in TypeScript: Essential Updates, Advantages, and Tips for 2024
Argho Dev -
How do you take the first steps in a daunting and complicated software project?
Ben Halpern -
The story behind John Doe
Muhammad Essa -
From Idea to Launch: Mistakes to avoid in your Development process
Rose Michelle -
Top comments (4)
A lambda is an anonymous function. Let's break down what that means:
first, you know what a function is: input, output. Going back to math a function 'f' could be defined as:
Make for a simple python function:
x is the input and the output is x-squared and the name of the function is f.
Turns out if you rename 'f' to anything, like 'g' or 'whositwhatsit', it doesn't matter, it's still the same.
So if it can be called anything and we are never going to need its name, then we can just call it nothing: it's anonymous. this is indicated by the 'lambda' keyword.
This is typically done with things that accept a function and do something with it. An example is 'map' which takes a list and a lambda, it applies the lambda to each item in the list, and returns the list of all the outputs
Finally, you can actually assign a lambda to a variable if it makes it easier or you need to reuse the lambda.
More info: python-course.eu/lambda.php
Lambdas in python are like normal functions but without names and with some random restrictions and they are hard to read.
I would recommend not using them. Python is not a Functional language, this was made clear as a design choice from 2 to 3.
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.
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.