E731 do not assign a lambda expression, use a def
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
E731 is a style warning from Python linters such as pycodestyle and flake8. It appears when you assign a lambda to a name, because once a function has a stable name, a normal def is usually clearer, easier to debug, and more consistent with Python style.
What Triggers E731
The warning appears for code like this:
That code works, but it is discouraged because the function is no longer anonymous in practice. You gave it a name, so the more readable form is:
The behavior is similar, but the def version communicates intent more directly.
Why Linters Prefer def
The main reason is readability. Python style generally favors explicit names and conventional function definitions over clever compact syntax.
A def also gives you advantages that become important as code grows:
- a better function name in tracebacks
- a natural place for a docstring
- room for type hints
- easy expansion if the logic becomes more complex later
Compare these two examples:
The second version is easier to understand, easier to document, and easier to extend.
When lambda Is Still Appropriate
The warning does not mean lambda is bad. It means assigned lambdas are usually the wrong tool. Inline lambdas are still fine when the function is short and used only once.
For example, sorting with a small key function is a good use case:
Likewise, a tiny transformation passed to map or filter can be reasonable, although many teams still prefer comprehensions for readability.
The key distinction is this: inline lambda keeps a short throwaway function close to the call site, while name = lambda ... creates a named function in a less clear form.
Refactoring an Assigned Lambda
Suppose you start with this code:
The linter wants this instead:
That looks like a small change, but it scales better. If you later need validation or logging, you can expand the function naturally:
With the lambda form, that evolution is awkward because lambda supports only a single expression.
Debugging Differences
One practical reason to prefer def is stack traces and introspection. Assigned lambdas still carry the internal name "<lambda>", which can make debugging less informative.
That prints "<lambda>".
With a normal function:
That prints "square", which is more useful in logs, debuggers, and profiling output.
A Good Rule of Thumb
If the function deserves a reusable name, use def. If it is a very small, local piece of behavior passed directly into another function, lambda may still be appropriate.
In modern Python, list comprehensions and generator expressions often replace simple map and filter cases anyway, which means many codebases use lambda less frequently than they used to.
Common Pitfalls
A common mistake is treating lambda as a shorter version of def in all situations. It is shorter, but not always clearer.
Another pitfall is assigning a lambda because the logic is currently tiny, then gradually growing the code around it. That creates a style warning now and a maintainability problem later.
Developers also sometimes think the warning is about performance. It is not. lambda and def are usually chosen for clarity, not speed.
Finally, do not “fix” E731 by disabling the linter unless your team has a deliberate style exception. In most codebases, replacing the assigned lambda with def is the better long-term choice.
Summary
- '
E731warns against assigning alambdato a name.' - If the function has a reusable name, prefer
def. - '
defimproves readability, debugging, documentation, and future expansion.' - Inline
lambdais still fine for short throwaway callbacks. - The warning is about code clarity and maintainability, not raw performance.
Related reading
- Eager execution in Tensorflow 2
- Edit Distance in Python
- Edit tensorflow inceptionV3 retraining-example.py for multiple classificiations
- Efficent way to split a large text file in python
- Efficient manipulation of a list of cartesian coordinates in Python
- Efficient way to apply multiple filters to pandas DataFrame or Series
- Efficient way to apply multiple filters to pandas DataFrame or Series
- Efficient way to rotate a list in python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.