Anonymous functions with lambda#
So far, we’ve used def
to define functions. It can get a little silly when the functions are short, like our get_id
and get_name
examples. Python supports anonymous functions using the lambda keyword (borrowed from the lambda calculus). Here’s an example:
Notice that we don’t need to write a return
here: when using lambda
, you just write an expression, and that’s what is returned. If you find yourself writing a lambda
that does a lot… then it’s time to break out def
and write a fuller function. Lambda functions are best for short bits of code, like when calling map
or filter
or list.sort
: