In Python, a function is an object like any other object. You can store it in a list, pass it to another function, return it from a factory, and even attach attributes to it. lambda is simply the short way of building one of those objects in place, which makes it a tool for expressing small transformations and not a shortcut for skipping def.
This is the first lesson in the Python Decorators, Closures and Generators module, and the next lesson in this module is about "Closures: Functions That Remember".
What does a lambda expression actually create?
The lambda keyword builds a function object that has no name, no docstring, and no statements beyond a single expression. It is exactly the same kind of object that a def creates, and you can see that by inspecting it.
def add(a, b):
"""Add two numbers."""
return a + b
double = lambda n: n * 2
print(add.__name__) # add
print(double.__name__) #
print(add.__doc__) # Add two numbers.
print(callable(double)) # True
print(double(21)) # 42
Notice the missing name. A lambda has no name of its own, which is why tracebacks show <lambda> and why an error inside one is hard to track down. It is also why a lambda cannot contain a docstring, cannot contain a statement such as a for loop or a block of if, and cannot be given a docstring at all.
Give a function a name and you gain three things for free. You get a meaningful entry in a traceback, a place to attach a docstring, and the ability to call it from anywhere else without passing it around. That is why a def with a short body is not more verbose than a lambda in any meaningful sense. It is only longer on the screen.
The practical rule that falls out of this is simple. Use a lambda when the function is small, obvious, and used right where it is written. The moment that it needs a name, a docstring, more than one expression, or a conditional branch, write a def instead.
Where do functions as values actually earn their keep?
The biggest win is passing behaviour into code that already knows how to apply it. Sorting is the everyday example, because the key argument takes a function and applies it to every element before it compares them.
class Runner:
"""A runner and the time that they took."""
def __init__(self, name, seconds):
self.name = name
self.seconds = seconds
runners = [Runner('ada', 12.4), Runner('grace', 11.9), Runner('linus', 12.1)]
by_time = sorted(runners, key=lambda runner: runner.seconds)
for runner in by_time:
print(runner.name, runner.seconds)
print('fastest:', min(runners, key=lambda runner: runner.seconds).name)
The loop prints the runners in the order of their finishing time, so grace comes first, then linus, and then ada. The last line asks the same question in a different way and returns grace as well. The standard library also offers operator.attrgetter, which does the same job as key=lambda runner: runner.seconds. It is a little faster because it is written in C, and it is a good choice when the key is one plain attribute.
The second everyday win is a higher-order function, which is simply a function that takes another function as an argument.
def apply_twice(value, func):
"""Apply func to value, and then apply func to the result."""
return func(func(value))
print(apply_twice(3, lambda n: n * 2)) # 12
print(apply_twice(3, lambda n: n + 5)) # 13
Every callback-driven library that you will meet works this way. map, filter, sorted, the default hook of json.dumps, and the event handlers of a graphical interface all take a function and call it for you. The lambda is simply the shortest way of handing over that behaviour.
There is one caution about map and filter. A list comprehension is usually clearer than either of them, because [f(x) for x in items] is easier to read than list(map(f, items)), and it is usually faster as well, because it avoids the extra layer of calls. Reach for map when the function already exists as a named object and a comprehension would add nothing.
How do you bind arguments without writing a new function?
When you want "the same function, but with one argument already filled in", functools.partial does it for you without you writing a wrapper at all.
from functools import partial
def connect(host, port, timeout=5.0):
"""Return a description of the connection."""
return host + ':' + str(port) + ' timeout=' + str(timeout)
local = partial(connect, 'localhost')
print(local(8080))
print(local(8080, timeout=1.5))
print(local.func.__name__)
print(local.args)
A partial is a real function object and not a trick with strings. local.func is the original connect function, and local.args is the tuple of the arguments that are already bound. Keyword arguments can be overridden at call time, which is why local(8080, timeout=1.5) gives a different timeout from local(8080).
For the common "sort by a key" case, the standard library offers a family of shortcuts in the operator module. There is attrgetter for attributes, itemgetter for dictionary keys and tuple elements, and methodcaller for calling a method.
Should you use a lambda or a def?
| Situation | Use | Why |
|---|---|---|
A one-line key for sorted or max | lambda | Nothing else fits on the line, and the key is obvious from the context. |
| Passing behaviour to a callback | Either | Use a lambda inline for one-off behaviour, and use a def when the same callback is passed in several places. |
| Anything with a condition or a loop inside | def | A lambda cannot hold a block, and squeezing a branch into a conditional expression hurts more than it saves. |
| Anything that you will want to debug later | def | Tracebacks show <lambda> instead of a name and a line number. |
| Returning a function from a factory | def | The returned function deserves its own name, its own docstring, and its own tests. |
There is one more trap worth naming here. A lambda closes over variables by reference and not by value, so if the variable changes later, the lambda sees the new value. That is the subject of the next lesson, and it is the single most common source of "why does my callback use the wrong value" bugs.
Quick reference
- A lambda is an unnamed function object with a single expression, so it has no statements, no docstring, and no name.
- Pass functions to
sorted,min,max,map,filter, and to any callback API. - Use
operator.attrgetterfor plain attribute keys, and a lambda when the key needs computing. - Use
functools.partialto pre-bind arguments instead of writing a wrapper function. - Promote any lambda that needs a name, a docstring, or a branch into a real
def.
Try it yourself: Write a list of five dictionaries, and give each one a name key, a score key, and a joined key. Sort the list by score with a lambda, and then sort it by name with operator.itemgetter. Next, build a partial from a function that takes a base and an exponent, and use it to produce three different powers. Finally, take the lambda that you wrote first and convert it into a def, and then decide which of the two versions you would rather maintain.