0
isn't use of lambda function is against the rules.
Functions are to be created with the piece of code that is to be used multiple time. however lambda functions are to be used at once, so logically they don't even qualify to be function. Do you have any strong reason why to use lambda functions?
4 ответов
+ 2
Lambdas don't have to be used only once. The only difference between a lambda and a named function is the lack of a name. Both can be passed around as objects and executed when needed.
Example of lambda executed twice:
f = lambda x: x * x
one = f(3)
two = f(4)
+ 2
@Ali: I'm contrasting:
f = lambda x: x * x
with:
def named_function():
return x * x
f = named_function
+ 1
@Igor
isn't f = lambda x: x * x
name the lambda as f() so it is no more anonymous.
+ 1
Moreover, there is no rule that named functions must be used more than once. If you have distinct piece of functionality implementing a requirement, putting it into a separate function makes the code more readable, function name makes the code more self-documenting, and the implementation becomes more testable. All these benefits apply even if the function is called only once.