Today is a great day to talk about #Python lambdas!
Let's learn about them in a thread
๐๐งต๐
A Python lambda is a one-line anonymous function.
Rule #1 is do NOT assign a lambda to a variable.
When you do that, it's no longer an anonymous function. You should just create a regular function at that point
If you want to run a lambda in your REPL, wrap it in parentheses, like this:
Lambdas can be found in a lot of functional programming constructs.
One popular use case is to use a lambda inside of Python's map() function:
A lot of people like to use lambdas with Python's sorted() built-in.
It's a handy way to add a simple key function:
Tkinter, a GUI package that comes with Python, uses lambdas for event handlers.
Here's an example:
You can return a lambda from a function. This can be a useful way to create a factory type function
You may recognize this coding pattern from decorators as well