DEV Community


Posted on

what are lambda functions in python

Top comments (4)

val_baca profile image
Valentin Baca

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:

This is not python. This is math.
f(x) = x*x

Make for a simple python function:

def f(x):
  return x * x

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 python, but it looks a lot like the math from before
lambda x : x * x
# this ^ x is the input. Can have more inputs separated by commas
# the colon ':' separates the input variables with the output
# the output is the result of what's to the right, in this case 'x*x'

# this defines the same lambda
# the name of the variables doesn't matter much either as long as it's consistent
lambda side : side * side

# Though x and y are common, make sure it's obvious from the variables what's going on

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

squares = map(lambda x : x * x, [0, 1, 2, 3, 4])

Finally, you can actually assign a lambda to a variable if it makes it easier or you need to reuse the lambda.

f = lambda x : x * x
squares = map(f, [0, 1, 2, 3, 4])
# squares will be [0, 1, 4, 9, 16]

big_squares = map(f, [100, 2000, 30000])
# big_squares will be [10000, 4000000, 900000000]

More info:

andrewlucker profile image
Andrew Lucker

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.

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.

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.