Find more articles in our new Python Knowledge Base

Assigning a lambda expression to a variable

The sole advantage that a lambda expression has over a def is that the lambda can be anonymously embedded within a larger expression. If you are going to assign a name to a lambda, you are better off just defining it as a def.

From the PEP 8 Style Guide:

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

The first form means that the name of the resulting function object is specifically ‘f’ instead of the generic ‘<lambda>’. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)

Anti-pattern

The following code assigns a lambda function which returns the double of its input to a variable. This is functionally identical to creating a def.

f = lambda x: 2 * x

Best practice

Use a def for named expressions

Refactor the lambda expression into a named def expression.

def f(x): return 2 * x