It's kind of a rule (or convention), if you follow the style guide, not to use lambda
in the context you have used. The reason for this is exactly what made you turn to the internet in confusion. The flags are:
- You are giving an anonymous function a name
- There are multiple
lambda
. For a language that does not have nested anonymous functions, this is a code smell. There must be a better way
How about writing this way and reaping the benefits:
def function_product(F, m): # returns inner, a function
def inner(x): # takes x, and closes over F and m from
return F(x)*m # outer scope, hence a closure
return inner
See, everything is clear because it's more readable now. Avoid lambda
, maybe except for callbacks,like in sorted()
. Normal functions are objects, just like 10
and hello
are.
Always do import this
. :)