How can you return a lambda function from another function (closure)?

Python interview question for Advanced practice.

Answer

return lambda x: x + n

Explanation

You can return a lambda directly. If n is in the outer scope, the lambda captures it. Example: def multiplier(n): return lambda x: x n.

Related Questions