Python logoPython/
PYL-W0108

Unnecessary lambda expressionPYL-W0108

Major severityMajor
Anti-pattern categoryAnti-pattern

A lambda that calls a function without modifying any of its parameters is unnecessary. Python functions are first-class objects and can be passed around in the same way as the resulting lambda. It is recommended to remove the lambda and use the function directly.

It is preferred to use the function hash directly as the lambda function calls the same function without any modifications. Another reason to avoid the lambda function here is that it makes debugging difficult. Lambdas show as <lambda> in tracebacks, where functions will display the function’s name.

Bad practice

items = [1, 2, 3]

hashify = lambda x: hash(x)
hashed_items = map(hashify, items)
items = [1, 2, 3]

# pass the builtin `hash` instead of the wrapper `hashify`
hashed_items = map(hash, items)